Aravind
Aravind

Reputation: 149

LTRIM function in C

I am trying to write a left trim function in C. Could someone please find out what is the mistake I am doing

int main()
{
  char string2[]="   wind";
  ltrim(string2);
  int new_len2=strlen(string2);
  printf("After trim String2 is <%s>\nLength is %d\n",string2,new_len2);
  return 0;
}

void ltrim(char *string)
{
 int i=0;
 while(string[i]==' ')
   {
    i++;
    string=string+i;
   }
printf("inside function string is <%s>---length is %d\n",string,strlen(string));
}

output:

inside function string is <wind>---length is 4
After trim String2 is <   wind>
Length is 7

Why the string is not changing in the main function after trimming.

Upvotes: 2

Views: 5446

Answers (4)

thexiv
thexiv

Reputation: 25

Quickest Answer Available it seems:

char * rtrim(char*);
char * ltrim(char*);

char * rtrim(char * h) {
    size_t s = strlen(h);
    char * string;
    int x = s;
    while (h[x] == ' ' && x >= 0)
    {
       string = string + h[x];
       x--;
    }
    printf("Inside rtrim() function string is <%s>---length is %d\n",string,strlen(string) - x);
    return string;
}

// We, as I had before, do not use sizeof() on strings,
// We have to use strlen because, as you may know
// we need to compensate for different sizes of types
// in certain platforms. Not all are static.
char * ltrim(char * h) {
    size_t s = strlen(h);
    char * string;
    int x = 0;
    while (h[x] == ' ' && x < s)
    {
       string = string + h[x];
       x++;
    }
    printf("Inside ltrim() function string is <%s>---length is %d\n",string,strlen(string) - x);
    return string;
}

Upvotes: 0

alk
alk

Reputation: 70981

An efficient approach would be to count the numbers of leading blanks and then move the string to the left exactly this number of characters in one go:

#include <ctype.h> /* for is blank() */
#include <string.h> /* for memmove() */

void ltrim(char * s)
{
  char * s_tmp = s;

  while (isblank(*s_tmp)) /* isblank() detects spaces and tabs. */
  {
    ++s_tmp;
  }

  memmove(s, s_tmp, s_tmp - s); /* Please note the use of memmove() here, as it
                                   allows the use of overlapping memory areas,
                                   which is not allowed for memcpy().
}

Upvotes: 2

wildplasser
wildplasser

Reputation: 44250

Minimalistic solution without using <string.h>

void ltrim(char *src)
{
char *dst;

    /* find position of first non-space character */
for (dst=src; *src == ' '; src++) {;}

    /* nothing to do */
if (dst==src) return;

    /* K&R style strcpy() */
while ((*dst++ = *src++)) {;}

return;
}

Upvotes: 4

user3121023
user3121023

Reputation: 8308

You can shift the contents of the string rather than move the pointer.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void ltrim(char *string);

int main()
{
    char string2[]="   wind";
    ltrim(string2);
    int new_len2=strlen(string2);
    printf("After trim String2 is <%s>\nLength is %d\n",string2,new_len2);
    return 0;
}

void ltrim(char *string)
{
    int i=0;
    while(string[0]==' ')
    {
        i = 0;
        while ( string[i]) {//shift the contents
            string[i]=string[i+1];
            i++;
        }
    }
    printf("inside function string is <%s>---length is %d\n",string,strlen(string));
}

Upvotes: 1

Related Questions