C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 18th, 2003, 01:16 AM
mulderGURL mulderGURL is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 9 mulderGURL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question 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;
}

Reply With Quote
  #2  
Old April 18th, 2003, 09:44 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
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.

Reply With Quote
  #3  
Old April 18th, 2003, 10:34 AM
mulderGURL mulderGURL is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 9 mulderGURL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb 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 *(^_^)*

Reply With Quote
  #4  
Old April 18th, 2003, 11:56 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
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.

Reply With Quote
  #5  
Old April 18th, 2003, 12:19 PM
mulderGURL mulderGURL is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 9 mulderGURL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;}

Reply With Quote
  #6  
Old April 18th, 2003, 01:15 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
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.

Reply With Quote
  #7  
Old April 18th, 2003, 01:51 PM
mulderGURL mulderGURL is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 9 mulderGURL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 *(^_^)*

Reply With Quote
  #8  
Old April 18th, 2003, 02:46 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,799 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 9 h 55 m 28 sec
Reputation Power: 437
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How To Split a string into words


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway