c - seg fault?

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;
}


Last edited on
Putting parentheses around a variable name does nothing. It cannot be made to bind any more tightly with itself!
 
            *(index)++;

Presumably you mean
 
            (*index)++;

Or, perhaps better:
 
            ++*index;  // doesn't need parentheses 


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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void char_extraction(const char* a, char* arr, int* index) 
{
    for (int i = 0; a[i]; i++)
        if (isalpha(a[i]))
            arr[(*index)++] = a[i];
}

int main()
{
    char str[] = "Ge34eks-f10or-Gee59ks";
    char array1[100] = "";
    int count1 = 0;
  
    char *token = strtok(str, "-");
    char_extraction(token, array1, &count1);
  
    while ((token = strtok(NULL, "-")) != NULL) 
    {
        array1[count1++] = ' ';
        char_extraction(token, array1, &count1);
    }

    for (int i = 0; i < count1; i++)
        printf("%c ", array1[i]);
    printf("\n");

    return 0;
}


Alternatively,

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char* char_extraction(const char* in, char* out)
{
    for ( ; *in; ++in) if (isalpha(*in)) *out++ = *in;
    return out;
}

int main()
{
    char str[] = "Ge34eks-f10or-Gee59ks";
    char arr[100] = "";

    char *token = strtok(str, "-");
    char *end = char_extraction(token, arr);
    while ((token = strtok(NULL, "-")) != NULL) 
    {
        *end++ = ' ';
        end = char_extraction(token, end);
    }
    *end = '\0';

    printf("%s\n", arr);

    return 0;
}

As yet another alternative:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

enum character_class { separator, digit, letter } classify(char c) 
{ return (c >= '0' && c <= '9')? digit: (c == '-')? separator: letter; }

int main()
{
  char str[] = "Ge34eks-f10or-Gee59ks";
  char *out = str, *beg = str, *end = str, *end_buf = str + sizeof str;
  
  for ( ; beg != end_buf; beg = end )
  {
    do ++end; while (end != end_buf && classify(*end) == classify(*beg));
    switch (classify(*beg)) 
    {
      case letter: do *out++ = *beg++; while (beg != end); break;
      case separator: *out++ = ' '; break;
      case digit: /* do nothing */; break;
    }
  }
  
  puts(str);
}


I would recommend studying @dizzydon's char_extraction in his second snippet. It's half of your assignment here, but it's also common and useful. C++ programmers know it generically as std::remove.
Last edited on
Looking at the final requirement, then why first split into tokens? Why not:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <ctype.h>
#include <stdio.h>

int main() {
	char str[] = "Ge34eks-f10or-Gee59ks";
	char out[100] = {0};

	for (char* s = str, *o = out; *s; ++s)
		if (isalpha(*s))
			*o++ = *s;
		else
			if (*s == '-')
				*o++ = ' ';

	puts(out);
}



Geeks for Geeks

Thanks for all the suggestions! The problem indeed was with the index. I will also be sure to look at the alternative versions.
Last edited on
Topic archived. No new replies allowed.