Thread: nested while loop

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    8

    Red face nested while loop

    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!

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Your seg fault lies in line 21.
    Code:
    array[ x ] != NULL;
    You need to change it or add NULL to your **array.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    8
    yey Thank you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with nested for loop .
    By perfectprashant in forum C Programming
    Replies: 5
    Last Post: 12-26-2012, 05:40 PM
  2. Help with for loop and nested if's
    By Gil Carvalho in forum C Programming
    Replies: 10
    Last Post: 06-14-2012, 11:20 AM
  3. help with nested loop
    By khoavo123 in forum C Programming
    Replies: 0
    Last Post: 01-23-2012, 01:29 PM
  4. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM