Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

strtok()

Posted on 2007-04-02
10
Medium Priority
?
605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 pointers 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.

715 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