The code itself is about typing a string and a character, and the program printing out what position the character is in the particular string.
What I want to add to this is to exit the program immediately when the word "end" is inputed, using strcmp().
The current problem is that although I type in the word "end", I need to type in a character in order for the program to end.
What I want is for the program to end immediately after typing in "end".
#include <stdio.h>
#include <string.h>
void input(char*, char*);
int strcheck(char*, char);
int main()
{
int count;
char str[100], ch;
while (1) {
input(str, &ch);
if (strcmp(str, "end") == 0) { //I'm guessing this part is the problem
break;
}
else {
count = strcheck(str, ch);
if (count == -1) {
printf("\"%s\" string doesn't have '%c'. \n\n", str, ch);
}
else {
printf("\"%s\" string has '%c' in position number %d .\n\n", str, ch, count + 1);
}
}
}
return 0;
}
void input(char* strp, char* chp)
{
printf("# enter string : ");
scanf("%s", strp);
printf("# enter character : ");
scanf(" %c", chp);
return;
}
int strcheck(char* strp, char chp)
{
int i;
int size = strlen(strp);
for (i = 0; i < size; i++) {
if (strp[i] == chp) {
return i;
}
}
return -1;
}
The result for this code comes out like...
# enter string : end
# enter character : a
But what I want is for it to...
# enter string : end //--> exit immediately