shaik786, I think you are removing the trailing spaces and not the leading...
One way to do this is have an extra pointer that points to the first character after the leading spaces:
Code:
int main(void)
{
char txt[] = " Hello world!\n";
char *ptr = NULL;
for(ptr = txt; *ptr == ' '; ptr++) ;
printf(ptr);
return 0;
}
Another way is to find the first character after the spaces and then copy the rest of the buffer to the beginning of the buffer (strcpy):
Code:
int main(void)
{
char txt[20];
char *ptr = NULL;
strcpy(txt, " Hello world!\n");
for(ptr = txt; *ptr == ' '; ptr++) ;
strcpy(txt, ptr);
printf(txt);
return 0;
}
Or use the memmove function:
Code:
int main(void)
{
char txt[20];
int len = 0;
int i = 0;
strcpy(txt, " Hello world!\n");
len = strlen(txt);
for(i = 0; txt[i] == ' '; i++) ;
memmove(txt, txt + i, len-i+1);
printf(txt);
return 0;
}