I tried to write a code that will look for a specific word inside a string and that will count it's place within the string . If the word doesn't exist inside the string, it should print that the word wasn't found. For example, for the sentence "I am late" , for "late" the result should be 3.
int count=0,i=0,j=0,k;
char word[30];
getchar();
gets(word);
k=strlen(word);
while(arr[i]!='\0'){
if(arr[i]==word[j]){
i++;
j++;
}
i++;
if(arr[i]==' ') // moves across a word in the string
count++; // count a word the index has passed
}
if(j==k) // if all letters were a match
printf("The word %s is placed in the %d place." , word , count);
else
printf("The word %s is not found." , word);
}
The problem is that for every sentence entered, it prints:
The word %s is not found.
I assumed that it skips for some reason the first part, and goes straight into word is not found
, but even after debugging I couldn't catch the moment and reason why it skips.