My program parses the string 'Ge34eks-f10or-Gee59ks' and splits them into tokens of three Ge34eks, f10or, Gee59ks. For each of this tokens the goal is to extract the characters and store them in an array, with a space in between the token characters. Ex: [G,e,e,k,s, ,f,o,r, ,G,e,e,k,s]
Issue: The for loop in char_extraction seems to only be printing twice for some reason. Any suggestions as to why and how to fix?
Edit: After attempting to use a debugger there seems to be a seg fault somewhere in char_extraction and the index though I am not sure why or specifically where
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char* char_extraction(char* a, char* arr, int* index)
{
char* p = a;
for( int i = 0; i < strlen(p); i++)
{
printf("%s\n", "test"); // ISSUE: ONLY PRINTS TWICE?
if(isalpha(p[i]))
{
arr[*index] = p[i];
//printf("%c\n", arr[*index]);
*(index)++;
}
}
return arr;
}
int main()
{
char str[] = "Ge34eks-f10or-Gee59ks";
int array[100] = { 0 };
char array1[100] = "";
int count1 = 0;
int* r = &count1;
// Returns first token
char *token = strtok(str, "-");
char_extraction(token, array1, r); // called once
count1 += 2;
// Keep printing tokens while one of the
// delimiters present in str[].
while ((token = strtok(NULL, "-")) != NULL)
{
char_extraction(token, array1, r); // should be called twice
count1 += 2;
}
for(int i = 0; i < count1; i++)
{
printf("%c\n", array1[i]);
}
//printf("%d\n", count);
return 0;
}
|