How to remove leading (ltrim()) blanks, newlines and tabs from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
  
#define LEN 30

void my_ltrim(char s[]);
  
int main(void)
{
    char s[LEN] = "   \n   \t  abcd efg";
      
    my_ltrim(s);
    printf("s = %s\n", s);
     
    return 0;
}
 
void my_ltrim(char s[]) 
{ 
    int i, j, in_first_char = 0;
    
    for (i = 0, j = 0; i < strlen(s); i++) 
    {
         if ( (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') || in_first_char == 1) 
         {
             s[j++] = s[i];
             in_first_char = 1;
         }
    }
    s[j] = '\0'; 
} 
  
/*
run:
    
s = abcd efg
 
*/

 



answered Nov 12, 2015 by avibootz
...