0

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.

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
  • 1
    Welcome to Stack Overflow! [DO NOT use `gets()`, it is dangerous](http://stackoverflow.com/q/1694036/2173917). use [`fgets()`](https://linux.die.net/man/3/fgets) instead. – Sourav Ghosh Dec 12 '16 at 17:06
  • Your code seems to be testing if the letters in `word` appear in the letters in `arr` -- but not necessarily contiguously. That isn't what you want, so you have a fundamental logic error in addition to whatever other bugs are in your code. Your code seems to be missing an inner loop. Alternatively, you could use `strtok()` to split on space and compare the strings returned from `strtok()` with the target word using `strcmp()`. Also --heed the warning of @SouravGhosh . – John Coleman Dec 12 '16 at 17:18
  • step through with a debugger – pm100 Dec 12 '16 at 19:25

1 Answers1

1

Note that i++ appears twice in the main loop, once conditionally and once unconditionally. The fact that it appears twice means that when a matching letter is found, i is incremented twice. The intention behind your code could be realized by getting rid of the conditional i++. Making this change and getting rid of the getchar() (which seems pointless from my point of view since it just discards the first letter of the input) and replacing gets by a not-quite-perfect use of fgets yields (with removed lines commented out):

#include <stdio.h>
#include <string.h>

int main(void){
    int count=0,i=0,j=0,k;
    char * arr = "I am late";
    char word[30];
    //getchar();
    fgets(word,30,stdin);
    strtok(word,"\n"); //trick for stripping off newline of nonempty line
    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);

    return 0;
}

When I run it and enter late I get the result:

The word late is placed in the 2 place.

Which seems to be almost what you want (there is an off-by-one error if you wanted the number 3). But, don't celebrate too soon since if you run it again with the input mate you get:

The word mate is placed in the 2 place.

Your code (once fixed this way) is really testing if the letters of the input word appear in order in arr, but doesn't check if the letters appear next to each other. You need to rethink your approach.

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • Also, when the two letters don't match, the OP should be setting j back to 0 to avoid the second issue you brought up about looking for the letters in order and not contiguously. – Chimera Dec 12 '16 at 22:21
  • eventually i got the result i desired, after adding a condition that word[j]!=EOF. thank for your answer – BellaconBud Dec 13 '16 at 16:40