3
I’m trying to do an exercise that tests if there’s any white space before a string and remove if you have, but is returning the following error:
Segmentation failure (recorded core image)
I can’t understand why.
Code:
#include <stdio.h>
void removerEspacosEsquerda(char string[]) {
int i, j, cont;
char *string2, a;
i = 0;
cont = 0;
while(string[i] != 0) {
if(string[i] == ' ') {
cont++;
}
i++;
}
i = 0;
j = 0;
printf("%d", cont);
while(string[i] != 0) {
if(i >= cont) {
string2[j] = string[i];
}
i++;
j++;
}
string = string2;
}
int main() {
char *string;
string = " teste";
removerEspacosEsquerda(string);
printf("%s", string);
}
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero