The task is to remove all of the extra whitespaces in a string, for example if a string looks like this:
volim OR
after the program is done it should look like this:
volim OR
so the program has to remove all the whitespaces before and after the strings, and those in between the words, so in the end there is just one between each word. This what I have so far :
#include <stdio.h>
char *IzbaciViskaRazmake (char *Str)
{
char *p = Str;
char *p2 = Str;
while(*p!='\0')
{
if(*p==' ')
p++;
else if(*p!=' ')
{
*p2 = *p;
p2++;
p++;
}
}
*p2='\0';
return Str;
}
int main() {
char tekst[] = " volim OR ";
IzbaciViskaRazmake(tekst);
printf("'%s'",tekst);
return 0;
}
The problem with my code is that it removes all of the whitespaces so it gives the output
volimOR
How can I repare my code, so it keeps the whitespaces between the words. PS- The use of pointers is a must.
*(s+1)is pointer arithmetic.