1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

evaluate a sentence.

Discussion in 'C' started by Brett.h, Dec 18, 2009.

  1. Brett.h

    Brett.h New Member

    Joined:
    Dec 18, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I am writing a program where the user inputs a string, and then that string is evaluated by the program word by word, but there is one problem, how do I break it down word by word, (i am also going to use vectors),

    -Brett
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Split it based on the spaces, comma, and fullstop
     
  3. dhakshinamoorthy

    dhakshinamoorthy New Member

    Joined:
    Dec 17, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Look at man pages for this function for extracting tokens from string

    strtok, strtok_r

    the sytax is
    char *strtok(char *str, const char *delim);



     
  4. Brett.h

    Brett.h New Member

    Joined:
    Dec 18, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Yes, this does work, but, how do I get Input from a user and evaluate that, not just a given statement already in the code?
    #include <stdio.h>
    #include <string.h>

    int main ()
    {
    char str[] ="- This, a sample string.";
    char * pch;
    printf ("Splitting string \"%s\" into tokens:\n",str);
    pch = strtok (str," ,.-");
    while (pch != NULL)
    {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
    }
    return 0;
    }
     
  5. dhakshinamoorthy

    dhakshinamoorthy New Member

    Joined:
    Dec 17, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    try this function

    fgets()


    ....
    this gets a string from user........
     
  6. learn3r

    learn3r New Member

    Joined:
    Dec 19, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    /root
    Home Page:
    the prototype of fgets() is:
    char *fgets( char *str, int num, FILE *stream );
    you should do:
    char buffer[100];
    .............
    fgets(buffer,sizeof(buffer),stdin);
    to take input from user.
     

Share This Page