TokenCounter of a string displaying not desirable results

  • Thread starter Thread starter TheMathNoob
  • Start date Start date
  • Tags Tags
    String
Click For Summary
SUMMARY

The forum discussion centers on a coding issue related to a token counting function in a program that processes graph data from a file. The user implemented a CountTokens function to count the number of tokens in the first line of input, which should only contain a single integer representing the number of vertices. However, when trailing spaces are present after the integer, the function incorrectly counts multiple tokens. The solution involves refining the tokenization logic to handle trailing spaces effectively.

PREREQUISITES
  • Understanding of C programming language syntax and functions
  • Familiarity with string manipulation functions such as strtok and strcpy
  • Knowledge of error handling in C, particularly using exit()
  • Basic concepts of graph theory, specifically vertices and edges
NEXT STEPS
  • Refine the CountTokens function to ignore trailing spaces effectively
  • Explore alternative string parsing techniques in C, such as using regular expressions
  • Learn about memory management in C, particularly regarding the use of malloc and free
  • Investigate unit testing methods for validating input parsing functions
USEFUL FOR

Programmers and software developers working with C who are involved in file parsing, especially those dealing with graph data structures and input validation.

TheMathNoob
Messages
189
Reaction score
4

Homework Statement


I am currently coding a program which reads a file and makes a graph based on the arrangement of the data in the file. The first line of the file stands for the number of vertices and the rest of the lines stand for the edges
something like this:
3
1 2
2 3
3 4
In the first line when I add a second number such as 4, so the first line looks like 3 4, my parseN function is supposed to display an error saying the first line is wrong hence there should be just one number. To achieve this, I use a counterToken function which counts the number of tokens in the first line. When I add this second number, the program works fine displaying the error, but then when I delete this second number and leave the text cursor 2 spaces away from the string in this case "3", my counterToken function begins to count 2 tokens all the time in the first line.

Homework Equations


Code:
int ParseN(char line[])
{
    int verticesN;
    int NumberOfTokens=CountTokens(line);
    if(NumberOfTokens!=1)
        {
            printf("bad firstline\n");
            fflush(stdout);
            exit(0);
        }

    char* pt=strtok(line," -");
     verticesN=atoi(pt);
    return verticesN;
}

int CountTokens(char line[])
{
    char clone[sizeof(line)];
    strcpy(clone,line);

          const char s[2] = " -";
          char *token;
          int number=0;
          token = strtok(clone,s);
          while( token != NULL )
          {
              number++;
             token = strtok(NULL,s);

          }
          free(token);

          return number;
}

The Attempt at a Solution

 
Last edited by a moderator:
Physics news on Phys.org
I think you're saying that if the first line contains a number with trailing spaces, your routine returns a count of two or more numbers?
 
NascentOxygen said:
I think you're saying that if the first line contains a number with trailing spaces, your routine returns a count of two or more numbers?
Yes
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K