The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How To Split a string into words
Discuss How To Split a string into words in the C Programming forum on Dev Shed. How To Split a string into words C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 18th, 2003, 01:16 AM
|
|
Junior Member
|
|
Join Date: Apr 2003
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
How To Split a string into words
Hi, my program is suppose to read in a line of string (in C, Not C++) and separate them into words and save it for different field (last name, first name... etc). but before i proceed to saving steps, i have problem separating them correctly, for example:
(basic UNIX)>>a.out
(my input) >>hi, Me A. 2154
it suppose to return:
hi
Me
A
2154
however, it doesn't work and instead, it would return me with:
hi
A
A
A
(which doesn't work so well when it checks for '(spaces)')
What is wrong? o_O? ( i tried to use strtok, but it won't even scan after the first space; and also this code looks kinda long -- this is only for the first part of separating the actual link of string would be contain more space in between: hi, Me A. 2415 teach 1982) THANK YOU so MUCH for helping me!
here is my code:
#include <stdio.h>
#include <string.h>
int main(void)
{
int i;
char message[50], *current, *last, *find;
gets(message);
last=message;
while((current=strchr(last, ','))!=NULL)
{ *current++='\0';
find=last;
last=current;
} /* while: scan ID number */
puts(find);
while((current=strchr(last, ' '))!=NULL)
{ *current++='\0';
find=last;
last=current;
} /* while: scan title */
puts(find);
while((current=strchr(last, '.'))!=NULL)
{ *current++='\0';
find=last;
last=current;
} /* while: scan salary */
puts(find);
while((current=strchr(last, ' '))!=NULL)
{ *current++='\0';
find=last;
last=current;
} /* while: check space */
while((current=strchr(last, '\n'))!=NULL)
{ *current++='\0';
find=last;
last=current;
} /* while: scan salary */
puts(find);
return;
}
|

April 18th, 2003, 09:44 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
Re: How To Split a string into words
I would use strtok, like you attempted to do before. You should be aware of exactly how it works, though. From MSDN:
On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.
|

April 18th, 2003, 10:34 AM
|
|
Junior Member
|
|
Join Date: Apr 2003
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
it works
wow, i try it with your instruction and it works this time!
i program as the following to test, and hopefully this is what you mean....
my program code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char message[50], *current, *last;
gets(message);
last=message;
current=strtok(last, ",");
puts(current);
current=strtok(NULL, " ");
puts(current);
current=strtok(NULL, ".");
puts(current);
current=strtok(NULL, " ");
puts(current);
current=strtok(NULL, " ");
puts(current);
current=strtok(NULL, " ");
puts(current);
return;}
>> May i just ask one more question, this is reading a line of string, n it works fine; if i am reading a file that contains lines of data, should i put a "while" loop at the beginning to test it until it reach "EOF". last time i try, it gave me a infinite loop... so... thanks again *(^_^)*
|

April 18th, 2003, 11:56 AM
|
 |
jasondoucette.com
|
|
Join Date: Feb 2003
Location: Canada
Posts: 378

Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
|
|
|
Great. However, it would be best to place the consecutive calls with NULL in a loop, so it can handle a string with any number of words.
strtok
Return Value
Returns a pointer to the next token found in strToken. They return NULL when no more tokens are found.
Last edited by Jason Doucette : April 18th, 2003 at 12:03 PM.
|

April 18th, 2003, 12:19 PM
|
|
Junior Member
|
|
Join Date: Apr 2003
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
wow, you respond so fast.... anywaz, i added the loop and it works fine EXCEPT... when it returns me the output the very last part is:
(input)>>Me, you S. 241 work 817.05
(output)>>Me
you
S
241
work
817.05
Segmentation fault
>>
-- it reads from a file of data, n that file contains several lines of strings. the very last string ends with a new line (nothing is after the new line), i tried to use "scanf" to test the EOF, problem become worse; it won't read the data and still ends with Seg.Fault..... (-_-#) Thank you again
code:
int main(int argc, char *argv[])
{
int n, index=1;
char message[100], *current, *last;
FILE *fpin, *fpout;
if((fpin=fopen(argv[index], "r"))==NULL)
{ printf("Data file couldn't be opened.\n");
return;
} /* if: test for successful file opening */
while(!feof(fpin))
{ fgets(message, 100, fpin);
last=message;
current=strtok(last, ",");
puts(current);
current=strtok(NULL, " ");
puts(current);
current=strtok(NULL, ".");
puts(current);
current=strtok(NULL, " ");
puts(current);
current=strtok(NULL, " ");
puts(current);
current=strtok(NULL, " ");
puts(current);
} /* while: scan until end of file */
return;}
|

April 18th, 2003, 01:15 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
you're overstepping the bounds and trying to access space that isnt there. try using a loop like this:
while(tokenPtr != NULL)
{
tokenPtr = strtok(NULL, " ");
}
this way, u always check to see if there is more to tokenize before u do the operation.
|

April 18th, 2003, 01:51 PM
|
|
Junior Member
|
|
Join Date: Apr 2003
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
but which part I should replace this with? if i replace "while(fpin!=NULL)" with "while(current!=NULL)" and then... it still return me with the same result... at the end it's still Seg.Fault.....
just one more question... how come it trys to access space and return me "seg.Fault" statement, but when it perform the test to the previous strings, it doesn't happen?
what is Segmentation fault actually representing?
Thank you~ you r helping me with linked list too~ Thanx *(^_^)*
|

April 18th, 2003, 02:46 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: Originally posted by mulderGURL
but which part I should replace this with? |
As I read what Infamous is telling you, you will replace from the first puts to the last one, inclusive. Though I would add one puts statement in the while loop:
Code:
while(current != NULL)
{
puts(current);
current = strtok(NULL, " ");
}
Quote: Originally posted by mulderGURL
what is Segmentation fault actually representing? |
That you are trying to access memory outside of the chunk of memory allotted you. This is most commonly due to trying to use a bad pointer value, such as NULL (that would hit the interrupt vector table, which can be a very bad thing for the entire system).
The reason why it was happening here is because strtok returns a NULL when there are no more tokens in the input string.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|