-
getline to divide
how would i?
I have array of string which have the following:
one meaning of one
two meaning of two
three meaning of three
For example, "one" is a word and "meaning of one" is a meaning of one. How would I divide each line into two pieces so I can have word and meaning separately?
I would like to use getline but don't know how to use it.
I want to use a first space to separate those into word and meaning.
Please let me know. thanks
-
Use strchr to find the first space in a string.
-
There are a number of ways to read up to the first space and then do something else. The easiest of which would be:
Code:
if ( fgets ( a, sizeof a, stdin ) != NULL ) {
if ( sscanf ( a, "%s %[^\n]", b, c ) == 2 )
printf ( "%s\n%s\n%s\n", a, b, c );
else
/* Report error and handle */
}
else
/* Report error and handle */
But a safer way would be to modify the input so that there is a delimiting character not in the real data which separates the strings that you want, like so:
one : meaning of one
You can then use the : as the separator. That way you don't have to worry about the spacing of the input, such as if someone forgets to put a space between "one" and "meaning":
one-meaning of one
This would result in "one-meaning" being the word entry and "of one" being the definition. Not quite what you want. ;)
-Prelude
-