0

im trying to tokenize some strings so that the numbers are tokens themselves so i can eventually add them but my tokenization is not working and im not sure why. It compiles correctly but when i execute the file it says "segmentation fault", anyone know why or how to get the number as tokens?

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

int stringSum(char *s);
/*void minMaxValues(char *s, int *min, int *max);*/

int main(void)
{
    int value,i;
    char* text[] = { "1 + 2 + 3 + 4",
                     "7",
                     "30 + 20 + 10",
                     "9 + 900 + 90000 + 9000 + 90" };
    for(i=0;i<4;i++) /*send strings to function*/
        stringSum(text[i]);
}

int stringSum(char *s)
{
    char* del = " + ";
    char* token;
    token = strtok(s,del);
    while(token != NULL)
    {
        printf("%s\n",token);
        token = strtok(NULL, del);
    }
}
Jovis13
  • 21
  • 1
  • 8

4 Answers4

1

strtok requires modifiable strings. You defined an array of non-modifiable string literals in main and therefore they won't work with strtok.

For a quick solution, use:

char* temp = malloc(strlen(s) + 1);
strcpy(temp, s);
token = strtok(temp,del);

This dynamically allocates a modifiable string with the same value as s that you can use in strtok.

Since this is a dynamically allocated variable, remember to release the memory at the end of the method:

free(temp);

Your revised stringSum method should now look like this:

int stringSum(char *s)
{
    char* del = " + ";
    char* token;

    char* temp = malloc(strlen(s) + 1);
    strcpy(temp, s);
    token = strtok(temp,del);

    while(token != NULL)
    {
        printf("%s\n",token);
        token = strtok(NULL, del);
    }

    free(temp);
    return 0; //or whatever it is that you want to return
}
Daniel Kleinstein
  • 5,262
  • 1
  • 22
  • 39
0

The function strtok modifies the original string. Try passing char s[] instead of char* s and

why did we use char s[]?

See the difference between char pointer and char array from here (What is the difference between char s[] and char *s?)

Community
  • 1
  • 1
Ayush
  • 2,608
  • 2
  • 22
  • 31
0
token = strtok(s,del);

In the specifications of strtok function, s has to be writeable but in your example it's a string literal.

char* text[] = { "1 + 2 + 3 + 4",
                 "7",
                 "30 + 20 + 10",
                 "9 + 900 + 90000 + 9000 + 90" };

All strings in the text array are string literals and are therefore unmodifiable.

ouah
  • 142,963
  • 15
  • 272
  • 331
0

it is illegal strtok to change the first argument is a string literal can not change.

Alternative :

#include <stdio.h>
#include <stdlib.h>

int stringSum(const char *s);

int main(void){
    int value, i, size;
    char *text[] = { "1 + 2 + 3 + 4",
                     "7",
                     "30 + 20 + 10",
                     "9 + 900 + 90000 + 9000 + 90"
    };
    size = sizeof(text)/sizeof(*text);
    for(i=0;i<size;i++)
        stringSum(text[i]);

    return 0;
}

int stringSum(const char *s){
    int sum = 0;
    char *endp;
    do{
        int num = strtol(s, &endp, 10);
        if(*endp == ' ' || *endp == '+' || *endp == '\0'){
            sum += num;
            if(*endp)
                s = ++endp;
        } else { //s has invalid character
            fprintf(stderr, "Illegal characters are included.\n");
            break;
        }
    }while(*endp);
    printf("%d\n", sum);
    return sum;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70