Solved

strtok()

Posted on 2007-04-02
10
600 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

816 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

12 Experts available now in Live!

Get 1:1 Help Now