1

So I'm new to C and am teaching myself string handling. My issue so far as I can tell is that my function that tokenizes sp? the input string of numbers is not only cleaving on the spaces. For instance: if I input a number like 45, the resultant string in my array would show both 45 and the 5, so in two digit numbers it is splitting the number for whatever reason. I've searched at length but have not had any luck.

Hopefully it's not a glaring error that I'm just overlooking. But I've reached the point where I can't continue with my learning so any help is appreciated!

Example output:

please enter your string: 1 45 30 82
converting strings to ints
Printing the string
 1, 0, 45, 5, 0, 30, 0, 0, 82, 2,
Press any key to continue . . .

My code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>

#define STRING_LENGTH 81
#define MAX_TOKENS 40

int StrInput( char dataStr[]);
void atoiWorker( char dataStr[], char results[], int idx);
void printer ( char dataStr[], int idx);
void tokenize ( char dataStr[], char results[]);

int main()
{
    int idx;
    char dataStr[STRING_LENGTH];
    char results[STRING_LENGTH];
    idx = StrInput(dataStr);
    tokenize(dataStr, results);
    atoiWorker(dataStr, results, idx);
    printer(results, idx);
}

int StrInput(char dataStr[])
{
    int idx = 0;
    printf( "please enter your string: " );
    while (idx < (STRING_LENGTH) && ((dataStr[idx] = getchar()) != '\n'))
        idx++;

    dataStr[idx] = '\0';
    return idx;
}

void atoiWorker( char dataStr[], char results[], int idx)
{
    int i;
    printf( "converting strings to ints\n" );
    for (i = 0; i < idx; i++)
        results[i] = atoi(&dataStr[i]);
}

void tokenize(char dataStr[], char *results[])
{
    int count = 0;
    char delim[] = " ,\t\n"; //found this on msdn, hopefully it's right

    if (results[0] = strtok(dataStr, " \t"))
        count++;

    while (results[count] = strtok(NULL, delim/*" \t"*/))
        count++;
}

void printer(char dataStr[], int idx)
{
    int i;
    printf( "Printing the string\n" );
    for (i = 0; i < idx; i++)
        printf( " %d,", dataStr[i] );
    printf( "\n" );
}
4
  • Show the complete code. Apr 22, 2013 at 23:48
  • 2
    how can this code compile? What is the actual type of results? You are assigning integers, char pointers, and chars to it's elements at different locations.
    – perreal
    Apr 22, 2013 at 23:53
  • It of course cannot compile because the two signatures for tokenize don't match.
    – Jim Balter
    Apr 23, 2013 at 1:05
  • It did compile, how else would I get an output? (Unless I'm not understanding that either :) going to work on making some changes. @perreal I was under the impression that atoi would change the separate strings inside of the array into ints? Apr 23, 2013 at 14:59

2 Answers 2

1

After setting results[] in tokenize() you then overwrite it in atioWorker().

1

fix like this

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>

#define STRING_LENGTH 81
#define MAX_TOKENS 40

void StrInput( char dataStr[]);
void atoiWorker( char *tokens[], int results[], int idx);
void printer ( int results[], int idx);
int tokenize ( char dataStr[], char *results[]);

int main()
{
    int idx;
    char dataStr[STRING_LENGTH];
    char *tokens[MAX_TOKENS];
    int results[MAX_TOKENS];
    StrInput(dataStr);
    idx = tokenize(dataStr, tokens);
    atoiWorker(tokens, results, idx);
    printer(results, idx);
}

void StrInput(char dataStr[])
{
    int idx = 0;
    printf( "please enter your string: " );
    while (idx < (STRING_LENGTH) && ((dataStr[idx] = getchar()) != '\n'))
        idx++;

    dataStr[idx] = '\0';
}

void atoiWorker( char *tokens[], int results[], int idx)
{
    int i;
    printf( "converting strings to ints\n" );
    for (i = 0; i < idx; i++)
        results[i] = atoi(tokens[i]);
}

int tokenize(char dataStr[], char *results[])
{
    int count = 0;
    char delim[] = " ,\t\n"; //found this on msdn, hopefully it's right

    if (results[0] = strtok(dataStr, " \t"))
        count++;

    while (results[count] = strtok(NULL, delim/*" \t"*/))
        count++;
    return count;
}

void printer(int result[], int idx)
{
    int i;
    printf( "Printing the string\n" );
    for (i = 0; i < idx; i++)
        printf( " %d,", result[i] );
    printf( "\n" );
}
2
  • So the main adjustment here is that I now have a separate array for atoiWorker to solve SpacedMonkey's answer that I'm overwriting the results array (why is this bad practice)? (just ran your code, works great, and am working through the differences) Apr 23, 2013 at 15:16
  • @t0rqued it isn't bad practice. tokenize() stores pointers to the start of each number in results[] so that atoiWorker() knows where each number starts. Instead, your code goes through the original string a character at a time, including the NULs, overwriting the very data it needs. Apr 25, 2013 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.