Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   evaluate a sentence. (http://www.go4expert.com/forums/evaluate-a-sentence-t20398/)

Brett.h 18Dec2009 06:03

evaluate a sentence.
 
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

shabbir 18Dec2009 09:08

Re: evaluate a sentence.
 
Split it based on the spaces, comma, and fullstop

dhakshinamoorthy 18Dec2009 09:43

Re: evaluate a sentence.
 
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);




Brett.h 19Dec2009 03:01

Re: evaluate a sentence.
 
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;
}

dhakshinamoorthy 19Dec2009 07:25

Re: evaluate a sentence.
 
try this function

fgets()


....
this gets a string from user........

learn3r 19Dec2009 11:45

Re: evaluate a sentence.
 
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.


All times are GMT +5.5. The time now is 09:04.