I have an assignment for my C class that I've finished. I think it should work, but it doesn't work for two cases. I cannot think of any reason for why it wouldn't work, especially since it worked for those two cases before I got the rest of the bugs out. I just need someone to find a case where the code breaks
The prompt:
Use the following keywords: programming, cpp, nba, yolo. Your program will first read input from stdin, discarding all non-alphabetic characters until 140 alphabetic characters have been read in (regardless of the number of characters that takes), or EOF is reached. Together, the letters you have read in form a word. Attempt to match the word you have read in to the key words (ignore case when matching). If successful, write the word (with each letter in its original case) followed by a newline character; otherwise output nothing
#include <stdio.h>
#include <ctype.h>
void match(char *pattern, char *collection, int ignoreCase)
{
}
int main()
{
//declare variables
char string[141] = {'\0'},
*keywords[] =
{
"programming", "cpp",
"nba", "yolo"
};
int size, c;
for ( size = 0, c = getchar(); c != EOF && size < 140; c = getchar())
if(isalpha(c)) string[size++] = c;
char **words;
for( words = &keywords[0]; words < &keywords[4]; ++words)
match(*words, string, 1); // or use strstr
return 0;
}
If you want to use the above code, you now have to fill in the function called match which should take 2 strings and try to find the first one inside the other. The parameter ignoreCase if set to 0 should only match strings that have the same lexicographic value in their respective permutations. Otherwise the function should match any 2 strings provided the characters are the same when converted to the same case
Enter over 140 characters into the string. At line 28: string[size] = '\0'; You are writing into string[140] - outside the bonds. It's stack corruption due to my IDE.
Thanks for pointing out my failure to allocate enough size, but I haven't solved it.
@Smac89
I can only use the stdio library, and I'm not supposed to find the words in the string. I'm only supposed to see if the string matches (as per the input/output examples).
I kind of need to get this finished by the end of the week. I don't have any clue what's wrong besides the 140/141 characters, which I fixed. I have tried the example inputs and they work. I have tried "nba" above, and it works. I can't figure out what cases it would break for, so I'm completely lost. I'm trying to get other perspectives, but nobody can figure it out yet? What am I missing?
Older versions of C do not have the bool type implemented into them. So to return bool, you have to use int. The #define mechanism used above is syntatic sugar for the user
I wish I knew those two cases. I can get it to work for all cases I test it with, but when it comes to turning it in, I don't get access to the tests. This makes it impossible for me to find out what those cases are. That's why I'm asking people to break it. When it breaks, I solve the problem and get the rest of my credit, hopefully. I had it working for those two cases specifically when the code wasn't working, but now that it works for the rest of the cases, those two don't.
There isn't much one can do for you at this point with the amount of information you have provided. I have tested the code with all(?) the input it is supposed to receive and it seems to work well. So here are some things you have to make sure of:
Are you sure that the input you have been given is all the input that the program is expected to process? (copy paste issues)
Your program only reads one input from stdin. Is the marker only running the program once with one input per run or is your program expected to read multiple lines of input at once?
You use the tolower function without including the header where it is defined (ctype.h). How do you know you are using the correct tolower function?
Lastly, if you feel that your program is functioning as expected, then maybe submit a different code and see what the result is. If you are interested in this option, I can give you the code or you can make another one yourself
The information I have given is almost literally word for word from my prompt.
All that input I gave was copied straight from the prompt and I have tested other values and they work.
My program is only supposed to read once.
I can only use the stdio.h header. I am not allowed to use anything else, but I have found this tolower function to work, so I don't think that to be an issue.
As a college assignment, I really don't want to get in trouble for cheating. even submitting your code for a test and not a final product will be considered cheating. I can't get in trouble for that.
I'm really just trying to find input that will not work, but if anybody sees some glaring mistake, I'd be happy to try.