How does strtok work in this case?

  • Thread starter TheMathNoob
  • Start date
  • Tags
    Work
In summary: A slight error: Your first line (char str[5]="jk kj";) has no room for a string terminator. You should use char *str="jk kj"; (or char str[]="jk kj";).
  • #1
TheMathNoob
189
4

Homework Statement


I have a file which has data set in this way
1
2 3
4 7
1 2
I have to make a graph with those values considering that the value of the first line corresponds to the vertices and the next two values per line to the edges. I am having troubles with a function that doesn't detect if the first line has two values or not. If it does, then it should displayed an error.

Homework Equations

[/b]
Code:
int ParseN(char line[])
{
    char* first;
    char* second;
    int true=1;
    int numberVertices=0;
    char* pt=strtok(line," -");
    printf("%s",pt);
    char* pt2=strtok(NULL," -");
    printf("%s",pt2);
    if(pt2==NULL) // in this case when I test my file with just one value in the first line, it doesn't detect the NULL
    {
        puts("yes");
    }
    else
        puts("no");
    return 1;
}

by the way sorry for not following the directions in the previous post. Now I understand what I have to do

The Attempt at a Solution

 
Last edited by a moderator:
Physics news on Phys.org
  • #4
TheMathNoob said:
My code is not detecting the null
Maybe because NULL is an invalid pointer (by definition).
 
  • #5
Svein said:
Maybe because NULL is an invalid pointer (by definition).
even the code in the website that you gave me doesn't work
Code:
char str[5]="jk kj";
    char s=" ";
    char* token;
    int counter=0;
    token = strtok(str,s);

      while( token != NULL )
      {
         printf( " %s\n", token );
         token = strtok(NULL,s);
      }    free(str);
 
  • #6
A slight error: Your first line (char str[5]="jk kj";) has no room for a string terminator. You should use char *str="jk kj"; (or char str[]="jk kj";).
 

Related to How does strtok work in this case?

1. How does strtok work in this case?

Strtok is a C library function used for splitting a string into smaller strings, known as tokens. In this case, strtok works by using a delimiter to split the original string into tokens, which can then be accessed individually.

2. What is the purpose of using strtok in this case?

The purpose of using strtok is to break down a larger string into smaller, more manageable pieces. This is useful for tasks such as parsing input or manipulating strings in a program.

3. How is the delimiter selected for strtok in this case?

The delimiter for strtok can be any character or sequence of characters that is used to separate the tokens in the original string. This can be specified by the programmer or defaulted to a space character.

4. Can strtok be used on strings with multiple delimiters in this case?

Yes, strtok can be used on strings with multiple delimiters. It will split the string into tokens at every occurrence of any of the specified delimiters.

5. Are there any limitations or potential issues when using strtok in this case?

One limitation of using strtok is that it modifies the original string, replacing the delimiter characters with null characters. This means that the original string cannot be used again without being reassembled. Additionally, if the delimiter characters are not consistent throughout the string, strtok may not work as intended.

Similar threads

Back
Top