473,413 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

strtok() not splitting string correctly

LucasBishop
Hi everyone; i'm working with a program that can't split a strig of characters if i introduce out of the program.
It only work's if the string is put it directly in the the soucer code, here it is:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<conio.h>
  4. #include<ctype.h>
  5. #include<stdlib.h>
  6.  
  7. int evaluar(char *simbolo)
  8. {
  9.  if(strcmp(simbolo,"el")==0 || strcmp(simbolo,"la")==0)
  10.   return 0;
  11.  if(strcmp(simbolo,"ni¤o")==0 || strcmp(simbolo,"ni¤a")==0 || strcmp(simbolo,"escuela")==0 || strcmp(simbolo,"casa")==0)
  12.   return 1;
  13.  if(strcmp(simbolo,"camina")==0)
  14.   return 2;
  15.  if(strcmp(simbolo,"lentamente")==0)
  16.   return 3;
  17.  if(strcmp(simbolo,"por")==0)
  18.   return 4;
  19.  if(strcmp(simbolo,"y")==0)
  20.   return 5;
  21.  if(strcmp(simbolo,"bonita")==0 || strcmp(simbolo,"lento")==0)
  22.   return 6;
  23.  if(simbolo=='\0')
  24.   return 7;
  25.  return 8;
  26. }
  27.  
  28. void main ()
  29. {
  30.  int tabla[7][8]={{1,6,6,6,6,6,6,6},
  31.           {6,2,6,6,6,6,6,6},
  32.           {6,6,3,6,0,0,4,10},
  33.           {6,6,6,5,6,6,4,10},
  34.           {6,6,3,6,0,6,6,10},
  35.           {6,6,6,6,0,6,6,6},
  36.           {6,6,6,6,6,6,6,-1}};
  37.  int entrada, col, estado=0, ind=0;
  38.  char *simbolo=NULL, cadena[80];
  39.  int flag=0;
  40.  clrscr();
  41.  printf("Introduzca la cadena -> ");
  42.  scanf("%s",cadena);
  43.  do
  44.  {
  45.   if(simbolo!=NULL)
  46.   {
  47.    simbolo=strtok('\0'," ");
  48.    col=evaluar(simbolo);
  49.   }
  50.   if(flag==0)
  51.   {
  52.    flag=1;
  53.    simbolo=strtok(cadena," ");
  54.    col=evaluar(simbolo);
  55.   }
  56.   switch(col)
  57.   {
  58.    case 0: entrada=0;
  59.        break;
  60.    case 1: entrada=1;
  61.        break;
  62.    case 2: entrada=2;
  63.        break;
  64.    case 3: entrada=3;
  65.        break;
  66.    case 4: entrada=4;
  67.        break;
  68.    case 5: entrada=5;
  69.        break;
  70.    case 6: entrada=6;
  71.        break;
  72.    case 7: entrada=7;
  73.        break;
  74.    case 8: printf("Caracter Incorrecto");
  75.        getch();
  76.        exit(0);
  77.   }
  78.    estado=tabla[estado][entrada];
  79.  if(estado==6)
  80.  {
  81.   printf("Error en la Cadena");
  82.   getch();
  83.   exit(0);
  84.  }
  85.  ind=ind+1;
  86.  }while(estado!=10);
  87.  printf("Cadena Correcta");
  88.  getch();
  89. }
I don't understand why only shows the frist split of the string, and then the variable simbol it converts into NULL.
May 1 '07 #1
13 2367
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. do
  2.  {
  3.   if(simbolo!=NULL)
  4.   {
  5.    simbolo=strtok('\0'," "); // Problem?
  6.    col=evaluar(simbolo);
  7.   }
Maybe because of this statement? Usually, after the first call, strtok is called like

Expand|Select|Wrap|Line Numbers
  1. simbolo=strtok(NULL, " ");
Instead, you are using the null character '\0'...maybe some confusion on your part? Try switching '\0' with NULL and see if your output is correct.
May 1 '07 #2
No, it gives me the same error; i don't know why but in this part:

Expand|Select|Wrap|Line Numbers
  1.  simbolo=strtok(NULL," ");
simbolo aquire the value NULL and then I only obtain the first split of the string. I think there's a trouble in the part where i writes the string, but I don't know why:

Expand|Select|Wrap|Line Numbers
  1.  printf("Introduzca la cadena -> ");
  2.              scanf("%s",cadena); //here, when i put a string in this variable just                               obtain the first split,
  3.               do
  4.                 {
  5.                  if(simbolo!=NULL)
  6.                  {
  7.                   simbolo=strtok('\0'," ");
  8.                   col=evaluar(simbolo);
  9.                  }
Instead, if i define the variable "cadena" form the begining, it works (like this):

Expand|Select|Wrap|Line Numbers
  1. char cadena[80]="el niño";
May 1 '07 #3
Savage
1,764 Expert 1GB
Have u tryed running it step by step?

It might be that part with !=NULL never gets executed for some reason

Savage
May 1 '07 #4
Yes, i've tried, but it doen't work.
May 1 '07 #5
Savage
1,764 Expert 1GB
Yes, i've tried, but it doen't work.
So those it get executed?

Savage
May 1 '07 #6
Yup, but it doesn't give the result that i want; it ggives me just the first split of he string, and the next splits become in NULL's.
May 1 '07 #7
Savage
1,764 Expert 1GB
I got it:


The problem is in scanf("%s",cadena).U have forgoten &operator in call.Also if problem persist after this use gets() instead and evrything should work

Savage
May 2 '07 #8
Jeeeez!!! you have right!!! I've forgetten the operator &, but the really answer was the gets(), THANKS A LOT, DUDE.
May 2 '07 #9
Savage
1,764 Expert 1GB
Jeeeez!!! you have right!!! I've forgetten the operator &, but the really answer was the gets(), THANKS A LOT, DUDE.
I'm more than happy to help u!!

Main reason why scanf didn't worked is (I think) that it ignores whitespaces and don't put them in array.That way ur pointer will allways point to the same location in string

Savage
May 2 '07 #10
Wait, i don't really understood the part when you say, that the pointer will always point to the same location in string. I think you refer to "cadena", right??
May 2 '07 #11
Savage
1,764 Expert 1GB
Wait, i don't really understood the part when you say, that the pointer will always point to the same location in string. I think you refer to "cadena", right??
Right.When called inside the function strtok() it can be named as a pointer.

Firt call to the function writes a null character just after the returned token.Other calls continue from there but becasue scanf ignored all whitespaces it would allways print out the same part of the string.

Savage
May 2 '07 #12
Ok, thats right, thanks!
May 2 '07 #13
Savage
1,764 Expert 1GB
And again:I'm more than happy to help u!!

Savage
May 2 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
6
by: nobody | last post by:
Hi: I am using Visual Studio .NET 2003 and console project (disabled precompiled header). I am using _tcstok() which resolves to strtok in my case. It keeps crashing (unhandled error, access...
5
by: tvn007 | last post by:
// I am using strtok to break the string // For example: to extract 5 from the string below: // TEST 1,P,5,PASS // Below is my code in C: ptr =strtok(testbuff,"...
13
by: ern | last post by:
I'm using strtok( ) to capture lines of input. After I call "splitCommand", I call strtok( ) again to get the next line. Strtok( ) returns NULL (but there is more in the file...). That didn't...
2
by: bluebeta | last post by:
Hi, I am using embedded visual C++ 4.0 and I want to use function strtok to split string into array. my sample code is as below: ...
8
by: hu | last post by:
hi, everybody! I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0. Program is simple, but mistake is confusing. First, the below code can get right outcome:"ello world, hello...
9
by: Zack | last post by:
The c99 standard states "The implementation shall behave as if no library function calls the strtok function." What exactly does this mean? Does this dictate any requirements to architectures...
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
8
by: Neel | last post by:
Hi friends, I 'm trying to extract values from a lines which are delimited by a space eg. content of string is: "Hello World" I use strtok to extract "Hello" and "World" the code I use is
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.