Error

Hello,

I`m doing a self study of the C programming language and now getting segmentation fault error when compiling this:

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

#include <stdio.h>


void squeeze(char s[], char p[]);

int main()

{

   char p[] = "Hello";
   char p2[] = "Yes";
   
   squeeze (p, p2);
   
   printf("%s\n", p);
   
   return 0;
}

   
   return 0;
}


void squeeze (char s[], char s2[])
{

  int i, j, x;
  
  for ( i = x = 0; s[i] != 0; ++i)
     {
       for ( j = 0; s2[j] != 0; ++j)     
        {
           if (s[i] != s2[j])
               s[x++] = s[i];
               s[x] = 0;
        }
     }
 }
       
    



The program needs to delete all characters in string s1 that match any character
in s2.
Line 22 and 23 are completely unnecessary. So just delete them.
Are you getting any other errors?
No, just that.
Are both line 36 and 37 supposed to be inside the if loop?
If so, inclose them with brackets. If there are no brackets, the if statement only extends to the first statement.

I didn't debug your code, but I think the problem is with your x variable, you might be incrementing past the bounds of s[] or s2[].
Try using a debugger or use print statements to see the value of x before you assign a value to s[x].
Last edited on
Well then, what is your idea?

I tried the following code with no success whatsoever, and I don`t understand why.

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

#include <stdio.h>


void squeeze(char s[], char p[]);

int main()

{

   char p[] = "Hello";
   char p2[] = "Yes";
   
   squeeze (p, p2);
   
   printf("%s\n", p);
   
   return 0;
}

   


void squeeze (char s[], char s2[])
{

  int i, j, x;
 
  
  for ( x,j = 0; s2[j] != '\0'; ++j)
     {
        
       for ( i = 0; s[i] != '\0'; ++i)     
        {
           
           if (s[i] = s2[j])                           /* Skip same elements */
              continue;

             s[x++] = s[i];
        }
     
     }
 
  s[x] = '\0';

}

 



It compiles OK but then...
Write many simple, small functions. Test each function to make sure it works correctly before moving on to the next one.Take baby steps. Particularly when you are learning programming.
(Ignore performance, unless it really is a constraint. 'When in doubt, use brute force'.)

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
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>

bool has_char( const char* cstr, char c )
{
    if( cstr != NULL )
        for( ; *cstr != 0 ; ++cstr ) if( *cstr == c ) return true ;
    return false ;
}

bool valid_pos( char* cstr, size_t pos ) // return true if pos is valid
{
    if( cstr != NULL )
    {
        size_t sz = 0 ;
        while( cstr[sz] != 0 ) ++sz ;
        return pos < sz  ;
    }
    return false ;
}

void remove_char_at( char* cstr, size_t pos ) // invariant: valid pos
{
    if( cstr != NULL )
    {
        assert( valid_pos( cstr, pos ) ) ; // nop if NDEBUG is defined
        for( ; cstr[pos] != 0 ; ++pos ) cstr[pos] = cstr[pos+1] ;
    }
}

char* squeeze( char* cstr, const char* filter )
{
    if( cstr != NULL && filter != NULL )
    {
        size_t pos = 0 ;
        while( cstr[pos] != 0 )
        {
            if( has_char( filter, cstr[pos] ) ) remove_char_at( cstr, pos ) ;
            else ++pos ;
        }
    }

    return cstr ;
}

int main()
{
    char cstr[] = "abcdefghijklmnopqrstuvwxyz0123456789" ;
    puts(cstr) ;

    const char filter[] = "abra3cadabra79kmtyz" ;
    puts(filter) ;

    puts( squeeze( cstr, filter ) ) ;
}

http://coliru.stacked-crooked.com/a/51aa0d05032045ad
Topic archived. No new replies allowed.