• C

strtok()

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;
}
ratheesh_kAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>>   pch = strtok (str," ,.-");

Add \t to the list :

  pch = strtok (str," ,.-\t");
0
ratheesh_kAuthor Commented:
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
Infinity08Commented:
What's the problem ? Can you describe it as accurate as possible - with example data, and code, and error messages ?
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

ratheesh_kAuthor Commented:
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
Infinity08Commented:
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
ratheesh_kAuthor Commented:
i m using the same above code .
0
Infinity08Commented:
No, I made a small modification to your code - namely on this line :

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
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
ratheesh_kAuthor Commented:
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
Infinity08Commented:
>> 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.