Hi I'm doing a program that ask a User to enter a sentence, then the program will look for the same words on the sentence from declared array of words.
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char string[100];
char word[100];
int i,namecount=0;
char *result;
char delims[] = " \n";
char *array[] = {"one", "two", "three", "four"};
int x;
printf("Type a string : ");
fgets(string,100,stdin);
result = strtok( string, delims );
while( result != NULL ) {
strcpy(word, result);
printf( "result is: %s\n", word );
for( x = 0; array[ x ] != NULL; x++ ){
if ( strcmp ( word, array[x]) == 0 )
printf( "The Same!.\n" );
}
result = strtok( NULL, delims );
}
}
OUTPUT:
Type a String: four three
result is: four
The Same!
Segmentation fault
I need to loop it but I can't figure it out! Please help!