I'm trying to trim a string (remove white spaces from the start & end of the string) using pointers for that.
char* killspace(char *a)
{
char *enda = NULL;
int i = 0, spaceS = 0, spaceE = 0, bigend = 0 , out = 0, taille = strlen(a);
do
{
if (a[i] == ' ')
{
spaceS++;
}
else
{
out = 1;
}
i++;
} while (out == 0);
out = 0;
do
{
if (a[taille] == ' ')
{
spaceE++;
}
else
{
out = 1;
}
taille--;
} while (out == 0);
bigend = (spaceE + spaceS);
// new size
enda = (char *)calloc((strlen(a)-bigend), sizeof(char));
i = 0;
for (int j = spaceS; j < (strlen(a)-spaceE); j++)
{
enda[i] = a[j];
i++;
}
return(enda);
free(enda);
}
bigend
is the number of whitespaces at the beginning and at the end of the string.
but the returned result had some random char like "ýýýý««««««««îþîþîþ"