I have a problem when I try to assign a character to my 'next' pointer, it throws me a segmentation error. With the debugger I've seen that next points to a character, the only thing I want to do is step on that character with a blank space (''). I do not understand why the error, since next is initialized with my pointer s1, which does not come NULL, but also corroborated eh that has not overflowed the chain.
int deleteChar(char *s1,const char *s2)
{
char *first = s1,
*last = s1,
*next = s1;
int accumulator = 0;
while(*next)
{
while(isWhite(*next) && *next)
next ++;
while(*next && !isWhite(*next))
{
if(isCharacter(*next, s2))
{
*next = ' '; //error de segmentacio
accumulator ++;
}
next ++;
}
while(last < next)
{
if(*last != ' ')
{
*first = *last;
first ++;
}
last ++;
}
*first = ' ';
first ++;
}
first --;
*first = 'int deleteChar(char *s1,const char *s2)
{
char *first = s1,
*last = s1,
*next = s1;
int accumulator = 0;
while(*next)
{
while(isWhite(*next) && *next)
next ++;
while(*next && !isWhite(*next))
{
if(isCharacter(*next, s2))
{
*next = ' '; //error de segmentacio
accumulator ++;
}
next ++;
}
while(last < next)
{
if(*last != ' ')
{
*first = *last;
first ++;
}
last ++;
}
*first = ' ';
first ++;
}
first --;
*first = '%pre%';
return accumulator;
}
int isCharacter(const char c, const char *s)
{
while(c != *s && *s)
s ++;
return c == *s ? 1 : 0;
}
#define isWhite(X) ((X) == ' ' || (X) == '\t')
int main()
{
char *s1 = {"algo que se me ocurrió"},
*s2 = {"aeiou"};
int cant;
printf("Prueba eliminar caraacter\n");
puts(s1);
puts(s2);
cant = deleteChar(s1,s2);
printf("La cadena modificada es: %s",s1);
printf("Cantidad de caracteres eliminados: %d",cant);
return 0;
}
';
return accumulator;
}
int isCharacter(const char c, const char *s)
{
while(c != *s && *s)
s ++;
return c == *s ? 1 : 0;
}
#define isWhite(X) ((X) == ' ' || (X) == '\t')
int main()
{
char *s1 = {"algo que se me ocurrió"},
*s2 = {"aeiou"};
int cant;
printf("Prueba eliminar caraacter\n");
puts(s1);
puts(s2);
cant = deleteChar(s1,s2);
printf("La cadena modificada es: %s",s1);
printf("Cantidad de caracteres eliminados: %d",cant);
return 0;
}