Solved

strtok()

Posted on 2007-04-02
10
598 Views
Last Modified: 2008-02-01
here is a program  to make tokens whenever there is a  comma , fullstop space . i want to include tab also in the list . That is , when there is a tab also ,it should make token . How can i make it  ?

#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;
}
0
Comment
Question by:ratheesh_k
  • 6
  • 4
10 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18841411
>>   pch = strtok (str," ,.-");

Add \t to the list :

  pch = strtok (str," ,.-\t");
0
 

Author Comment

by:ratheesh_k
ID: 18841423
Actullay i am  copying some data from Excel sheet and pasting it in notepad . I am reading the file line by line and feed to the program . But there is a problem . I tried with \t also . ..
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18841433
What's the problem ? Can you describe it as accurate as possible - with example data, and code, and error messages ?
0
 

Author Comment

by:ratheesh_k
ID: 18956479
i copy pasted from excel sheet to notepad ,and

12 34 56  78
66 23 44 53

i use strtok to read the file and make it tokens line by line . But when i typed all these in notepad ,there is no prob . But when i copy paste ,its behaving deiffernet ,,,,
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18956619
In your example, I see at least once that there are two spaces next to each other. You need to take that possibility in account.

ie. if your token is of size 0, then get the next ... or something similar.

#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)
  {
    if (strlen(pch)) printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ratheesh_k
ID: 18956700
i m using the same above code .
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 50 total points
ID: 18956711
No, I made a small modification to your code - namely on this line :

    if (strlen(pch)) printf ("%s\n",pch);
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18956713
If that doesn't fix your problem, can you tell me what difference you see between the text file you created manually and the text file generated from Excel ?
0
 

Author Comment

by:ratheesh_k
ID: 18956727
that maynot be the problem ,when i type a txt file ,i an keep enough space bet two numbers . That is not a problem . i hexdumped the two files .

In one ,trhere is a (ascii of \t ) . In other one there is not . So i included \t in strtok param . But still it is hing some problem .
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18956732
>> But still it is hing some problem .

Which problem ? Can you describe it ? Can you also show your exact code as well as example files ?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now