Thread: I made a c function and want to know how to find words in a sentence

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    I made a c function and want to know how to find words in a sentence

    First I will post my code. Basically I want to find words in a sentence instead of numbers from scanf. Just ignore the comments about fishy, they are code words I used to identify abstract concepts. The question is at the bottom of this post.

    Code:
    /* The idea is three parts
    1. is to get a function prototype, or empty address.
    2. is to fill that address with values from the main function.
    3. is to build a if, else if, else statement that checks to see if the norm is there.
    if it's too off course the "k.i.s.s" principle + "happy hiding fish" strategy is...*/
    
    #include <stdio.h>
    #include <conio.h>
    
    int process(int x, int y, int z);
    
    int main(void)
    {
    	int a;
    	int b;
    	int c;
    	int d;
    	printf("enter three numbers: ");
    	scanf("%d%d%d", &a, &b, &c); 
    
    	/* Checks value of a for subject or exclamation*/
    
    	if(a>9)
    	{
    	a = 100;
    	}
    	else if(a == 9)
    	{
    	a=9;
    	}
    	else
    	{
    	a = 0;
    	}
    	
    	/* Checks value of b for verb or exclamation*/
        if(b>9)
    	{
    	b = 100;
    	}
    	else if(b == 9)
    	{
    	b=9;
    	}
    	else
    	{
    	b = 0;
    	}
    	
    	/* Checks value of c for complement or exclamation*/
    	if(c>9)
    	{
    	c = 0;
    	}
    	else if(c == 9)
    	{
    	c=9;
    	}
    	else
    	{
    	c = 0;
    	}
    
    	/* Run process to get sum*/
    
        d = process(a, b, c);
    
    	/* checks for subject verb complement, subject verb, or exclamation, or oddity*/
    
        if((a==9)&&(b==9)&&(c==9))
    	{
    	printf("subject verb complement: %d", process(a, b, c) );
    	}
    	else if((a==9)&&(b==9)&&(c==0))
    	{
    	printf("subject verb: %d", process(a, b, c) );
    	}
    	else if((a==0)&&(b==0)&&(c==9))
    	{
    	printf("exclamation: %d", process(a, b, c) );
    	}
    	else 
    	{
    	printf("sad fishy: %d", process(a, b, c) );
    	}
    	/* gets you to type key to close program */
    
    		getch();
    		return 0;
    	}
    
    /* function that adds set to d in the main function */
    
    		int process(int x, int y, int z)
    		{
    		return x + y + z;
    		}
    I got this code below from MSDN library:

    Code:
    // crt_strtok_s.c
    // In this program, a loop uses strtok_s
    // to print all the tokens (separated by commas
    // or blanks) in two strings at the same time.
    //
    
    #include <string.h>
    #include <stdio.h>
    
    char string1[] =
      "A string\tof ,,tokens\nand some more tokens";
    char seps[]  = " ,\t\n";
    char *token1,
       *next_token1;
    
    int main( void )
    {
    
      // Establish string and get the first token: 
      token1 = strtok_s( string1, seps, &next_token1);
    
      // While there are tokens in "string1" or "string2"
      while (token1 != NULL)
      {
        // Get next token:
        if (token1 != NULL)
        {
          printf( " %s\n", token1 );
          token1 = strtok_s( NULL, seps, &next_token1);
        }
      }
    	getch();
    }

    Which gives this answer:

    A
    string
    of
    tokens
    and
    some
    more
    tokens

    Question:
    How do I split the string explicitly into variables, not in a loop, so I can then print the variables however I want.
    Last edited by jeremy duncan; 06-14-2011 at 09:39 PM.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I updated my first post with a new question. I got this idea for splitting from the links at the bottom of this page, so if you answer the question it might not only help me but other people using google.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Just assign the tokens to an array... it's not that hard.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Code:
    else if(a == 9)
    	{
    	a=9;
    	}
    The snippet above is useless for what should be obvious reasons.

    Not sure I understand what you are doing in that program but you are not doing it right.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Quote Originally Posted by CommonTater View Post
    Just assign the tokens to an array... it's not that hard.
    Code:
    // crt_strtok_s.c
    // In this program, a loop uses strtok_s
    // to print all the tokens (separated by commas
    // or blanks) in two strings at the same time.
    //
    
    #include <string.h>
    #include <stdio.h>
    
    char string1[] =
        "A string\tof ,,tokens\nand some  more tokens";
    char seps[]   = " ,\t\n";
    char *token1,
         *next_token1;
    
    int main( void )
    {
    	int totalWords = 0;
    
        // Establish string and get the first token: 
        token1 = strtok_s( string1, seps, &next_token1);
    
        // While there are tokens in "string1" or "string2"
        while (token1 != NULL)
        {
            // Get next token:
            if (token1 != NULL)
            {
                printf( "%d %s\n", totalWords++, token1 );
                token1 = strtok_s( NULL, seps, &next_token1);
            }
        }
    	getch();
    }
    Which gives this list:
    0 A
    1 string
    2 of
    3 tokens
    4 and
    5 some
    6 more
    7 tokens

    I wanted to use some if, else if, else on the list I get though but don't know how to do that? Would an array help with that?

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Quote Originally Posted by claudiu View Post
    Code:
    else if(a == 9)
    	{
    	a=9;
    	}
    The snippet above is useless for what should be obvious reasons.

    Not sure I understand what you are doing in that program but you are not doing it right.
    That code was only a proof of concept. here is what I want to do with the if, else if, else statements.
    Say I get a noun "House" or a verb "ran" in the list.

    The word is then used with a c boolean "or" operator. I check if noun==noun (house==house). If the word house is not equal with the noun being checked it goes to the next equality-testing in the list to see if the word is a noun.

    Then I can check stories for there nouns, verbs, etc.

    Code:
    if((house==car)||(house==eyes)||(house==boat)||(house==house)){
    printf("the word is a noun");
    }

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First of all the equality operator is not how you check strings for equality, strcmp() is.

    Secondly, unless the phrases you are reading have a deterministic structure, such as "The duck crossed the road" (i.e. noun, verb, noun) I don't see how you plan on determining what a word is. That is far from a trivial problem, unless, as I said you are using a limited dictionary.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I have my code to compare words now:

    Code:
    // crt_strtok_s.c
    // In this program, a loop uses strtok_s
    // to print all the tokens (separated by commas
    // or blanks) in two strings at the same time.
    //
    
    #include <string.h>
    #include <stdio.h>
    
    char string1[] =
        "house dog ran";
    char seps[]   = " ,\t\n";
    char *token1,
         *next_token1;
    
    int main( void )
    {
    
        // Establish string and get the first token: 
        token1 = strtok_s( string1, seps, &next_token1);
    
        // While there are tokens in "string1" or "string2"
        while (token1 != NULL)
        {
            // Get next token:
            if (token1 != NULL)
            {
                token1 = strtok_s( NULL, seps, &next_token1);
    			if ((strcmp(token1, "house")) || (strcmp(token1, "dog"))){
    	  printf("Ok, so it is one of the expected nouns.\n");
            }
    			if ((strcmp(1+token1, "run")) || (strcmp(1+token1, "ran"))){
    	  printf("Ok, so it is one of the expected verbs.\n");
            }
    		getch();
    	    return 0;
            }
        }
    }
    Which gives this answer;

    Code:
    Ok, so it is one of the expected nouns.
    Ok, so it is one of the expected verbs.
    As far as having the structure make sense I have a plan. Break the order into chunks of: subject, verb, object, or subject, verb, or exclamation word.
    This means I will need four if's: noun, verb, object, exclamation. Like the code below which has three if's.

    Code:
    // crt_strtok_s.c
    // In this program, a loop uses strtok_s
    // to print all the tokens (separated by commas
    // or blanks) in two strings at the same time.
    //
    
    #include <string.h>
    #include <stdio.h>
    
    char string1[] =
        "sam ate oranges";
    char seps[]   = " ,\t\n";
    char *token1,
         *next_token1;
    
    int main( void )
    {
    
        // Establish string and get the first token: 
        token1 = strtok_s( string1, seps, &next_token1);
    
        // While there are tokens in "string1" or "string2"
        while (token1 != NULL)
        {
            // Get next token:
            if (token1 != NULL)
            {
                token1 = strtok_s( NULL, seps, &next_token1);
    			if ((strcmp(token1, "house")) || (strcmp(token1, "sam"))){
    	  printf("Ok, so it is one of the expected subjects.\n");
            }
    			if ((strcmp(1+token1, "run")) || (strcmp(1+token1, "ate"))){
    	  printf("Ok, so it is one of the expected verbs.\n");
            }
    			if ((strcmp(1+token1, "run")) || (strcmp(1+token1, "oranges"))){
    	  printf("Ok, so it is one of the expected objects.\n");
            }
    		getch();
    	    return 0;
            }
        }
    }
    Which gives this answer:

    Code:
    Ok, so it is one of the expected subjects.
    Ok, so it is one of the expected verbs.
    Ok, so it is one of the expected objects.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I still have no idea quite where you want to go with this, but just based on where you're heading: you're going to have a lot of very unreadable if's, if you intend to write one statement that checks every noun in your dictionary, and every verb, etc. Also, if you start now, and write down every pattern (like subject+verb+object) for possible sentences, and you don't need a lot of sleep, you can probably finish by the end of the year.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I will try and see if I can make h files. One h file for verbs, one for subjects, one for objects. Then in the if statement just type the h file name Noun, Verb, Subject.

    The hard part will be adding the sentences to the function without hardcoding them in.
    I need to make a gui and be able to paste text into the gui, then have that be put into the function for the function to break it down into subject, verb, object groups.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    One thing that you will find out very soon is that it is hard to solve a problem, when you don't know the exact problem you are trying to solve.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    The problem I was trying to solve is done: step one.
    Step one was a way to identify the subject, verb, object in a sentence.
    Step two is to make the function input multiple sentences and identify each sentences subject, verb, object properties.
    So the function says sentence one has these properties, and sentence two only has these properties.
    After I get step 2 done I'll think up step 3.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by jeremy duncan View Post
    Code:
    // crt_strtok_s.c
    // In this program, a loop uses strtok_s
    // to print all the tokens (separated by commas
    // or blanks) in two strings at the same time.
    //
    
    #include <string.h>
    #include <stdio.h>
    
    char string1[] =
        "A string\tof ,,tokens\nand some  more tokens";
    char seps[]   = " ,\t\n";
    char *token1,
         *next_token1;
    
    int main( void )
    {
    	int totalWords = 0;
    
        // Establish string and get the first token: 
        token1 = strtok_s( string1, seps, &next_token1);
    
        // While there are tokens in "string1" or "string2"
        while (token1 != NULL)
        {
            // Get next token:
            if (token1 != NULL)
            {
                printf( "%d %s\n", totalWords++, token1 );
                token1 = strtok_s( NULL, seps, &next_token1);
            }
        }
    	getch();
    }
    Which gives this list:
    0 A
    1 string
    2 of
    3 tokens
    4 and
    5 some
    6 more
    7 tokens

    I wanted to use some if, else if, else on the list I get though but don't know how to do that? Would an array help with that?
    You use a loop.
    Code:
    char **sentenceWords;/*or char sentenceWords[N][M];*/
    int numWords;
    /*set up sentenceWords + numWords*/
    for(int i=0;i<numWords;++i){
      if( !strcmp(sentenceWords[i],"apple") ){
         puts("yummy apple
      } else if ( !strcmp(sentenceWords[i],"tire") ){
         puts("yukky tire");
      }
    }
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    There's three types of words in a sentence (Subject, Verb, Object), and two ways to represent them:
    1.) Subject, Verb, Object.
    2.) Subject verb, no object.

    The Object word is a single thing in a type of thing. Kitten is a type of cat: "I like kittens".
    Subject may be a single thing in a type of thing too but it probably isn't a object if it's followed by a verb.

    Code:
    If there is a order that shows what Type, there is a disorder that doesn't show what type: person 1.) "I ran." Person two.) "ran from what?" Person 1.) "I'm alive". Person two.) "What's going on?" Person one.) "I'm tired." Person two.) "sir are you ok?"
    This shows the use of subject verb from person 1 was a bit weird.
    A person using two words may be talking explicity, again a wierd use of terminology.

    This shows a quirk that may be tagged and that tag can be used to group and distinguish between normal and unnatural use of language/behavior.
    Last edited by jeremy duncan; 06-16-2011 at 12:08 PM. Reason: added "isn't a object"

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Really? What type would this sentence fall under in your taxonomy:

    "While driving to work I noticed a large camel on the side of the road".
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Split sentence into words
    By password in forum C Programming
    Replies: 4
    Last Post: 07-05-2010, 07:12 AM
  2. how to split a sentence to words
    By sivapc in forum C++ Programming
    Replies: 13
    Last Post: 09-28-2009, 01:21 AM
  3. Dividing sentence into words
    By Lord CyKill in forum C Programming
    Replies: 1
    Last Post: 11-02-2003, 07:25 AM
  4. How to count the words in sentence ?
    By Th3-SeA in forum C Programming
    Replies: 1
    Last Post: 10-01-2003, 01:34 AM
  5. Searching for words within a sentence
    By drdroid in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:09 AM