Break my code

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

Example Input/Output
Input     Output
NbA       NbA
 nbac
n#b a     nba
nba#yolo
NBC


My solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>

#define false 0;
#define true 1;

int main()
{
	//declare variables
	int c;
	int size = 0;
	int x;
	char string[141];
	int index = 0;
	char prog[] = "programming";
	char cpp[] = "cpp";
	char nba[] = "nba";
	char yolo[] = "yolo";
	int match = 1;
	int word = -1;

	while((c = getchar()) != EOF && size < 140)
	{
		if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
			string[size++] = c;

	}

	string[size] = '\0';

	for(x = 0; x < size && match; x++, index++)
	{
		if(index < 11 && tolower(string[x]) == prog[index])
		{
			if(word < 0)
				word = 0;
			if(word != 0)
				match = false;
		}
		else if(index < 3 && tolower(string[x]) == cpp[index])
		{
			if(word < 0)
				word = 1;
			if(word != 1)
				match = false;
		}
		else if(index < 3 && tolower(string[x]) == nba[index])
		{
			if(word < 0)
				word = 2;
			if(word != 2)
				match = false;
		}
		else if(index < 4 && tolower(string[x]) == yolo[index])
		{
			if(word < 0)
				word = 3;
			if(word != 3)
				match = false;
		}
		else
			match = 0;
	}

	if(word == 0 && index < 11)
	{
		match = false;
	}
	else if(word == 1 && index < 3)
	{
		match = false;
	}
	else if(word == 2 && index < 3)
	{
		match = false;
	}
	else if(word == 3 && index < 4)
	{
		match = false;
	}

	if(match)
	{
		for(x = 0; string[x] != '\0'; x++)
			putchar(string[x]);
		putchar('\n');
	}

	return 0;
}
Last edited on
Testing your code with the simple input:

dyolocppnbaprogramming


It failed to find any of the strings. Reason for this being the index matching you are trying to impose in the search

Here is how I would write the code you are trying to write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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
Last edited on
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.
Last edited on
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).
Oh yes, I now understand your program. I tested it with this input "nba". This did not work
Last edited on
How did you do "nba"? I tried just to be sure, and it worked for me?
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?
What inputs have you tried and it failed for them?
None that I input. That's the problem. I have no idea how to break it, so I need somebody to help me break it.
in your original post you said
I think it should work, but it doesn't work for two cases.


please tell us those 2 cases.
is there any need to define true and false in line 3 & 4?
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
k, i got it. thanx.
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.
Last edited on
Found one input where it does not work - No input:

1
2
gcc -o match test.c
./match <<< ""


Output is a newline character.

Here is the code, I replaced the newline with an "0" for you to see:
http://coliru.stacked-crooked.com/a/b6f1f578d7040c1b

This is also the case when no character is entered so something like:
"2342342\'23423423" will also produce the same output
Last edited on
Yes!!!

Thanks for all of your help! That was exactly the problem. Thank you so much for your help all.

This is exactly why I love this site.
Topic archived. No new replies allowed.