We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,687 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Implementing String trimming (Ltrim and Rtrim) in C

0
By Sanjay on Apr 30th, 2007 12:36 am

Here is a simple implementation of implementing a simple Left trim (ltrim) and Right trim(rtim) of unwanted characters from a C style string. (null terminated strings). Doesn't suffer the overheads of the memmove implementation in which the worst case scenario (a string containing all junk characters) is approx. N^2 / 2.

Comments, constructive criticisims are welcome.

#include <stdio.h>

char* rtrim(char* string, char junk);
char* ltrim(char* string, char junk);

int main()
{
    char testStr1[] = "     We like helping out people          ";
    char testStr2[] = "     We like helping out people          ";
    char testStr3[] = "     We like helping out people          ";
    printf("|%s|", testStr1);
    printf("\n|%s|", ltrim(testStr1, ' '));
    printf("\n\n|%s|", testStr2);
    printf("\n\n|%s|", rtrim(testStr2, ' '));
    printf("\n\n|%s|", testStr3);
    printf("\n|%s|", ltrim(rtrim(testStr3, ' '), ' '));
    getchar();
    return 0;
}


char* rtrim(char* string, char junk)
{
    char* original = string + strlen(string);
    while(*--original == junk);
    *(original + 1) = '\0';
    return string;
}

char* ltrim(char *string, char junk)
{
    char* original = string;
    char *p = original;
    int trimmed = 0;
    do
    {
        if (*original != junk || trimmed)
        {
            trimmed = 1;
            *p++ = *original;
        }
    }
    while (*original++ != '\0');
    return string;
}
/*

|     We like helping out people          |
|We like helping out people          |

|     We like helping out people          |

|     We like helping out people|

|     We like helping out people          |
|We like helping out people|

 */

it is a good, i like this type of programe

Nutty
Newbie Poster
1 post since May 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Nice one, thanks .

StrikerX
Newbie Poster
12 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

How to you handle tabs?

Ene Uran
Posting Virtuoso
1,830 posts since Aug 2005
Reputation Points: 676
Solved Threads: 255
Skill Endorsements: 7

> How to you handle tabs?
If you will notice, this is not a generic function, the way we have in Python, Ruby and other languages. You need to pass the character to be stripped. If you want to strip off tabs, pass the junk character to be '\t'.

~s.o.s~
Failure as a human
Administrator
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 54

Attention: rtrim could modify memory outside string range

Melando
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Attention: rtrim could modify memory outside string range

While you're correct, the bug report is unhelpful because you haven't specified when this would happen or suggested a fix. The problem stems from an unchecked decrement of original , which can drop below string if it's empty of contains nothing but junk characters.

If whatever memory prior to string also contains junk characters, the loop will continue and modify memory outside the bounds of the array. If memory prior to string doesn't contain junk characters, at least one element beyond the array will be accessed, which is still undefined behavior.

Trimming away the entire string is the special case that needs to be considered here, with awareness of three potential cases at original[0]:

  • original[0] is '\0': The string was empty, so nothing needs to be done.
  • original[0] is junk : The string is trimmed to nothing and must end at original[0] .
  • original[0] neither '\0' nor junk : The string must end after original[0] , as in the original algorithm.

Once the potential cases are discovered, it's a simple matter to account for them:

char *rtrim(char *string, char junk)
{
    char *original = string + strlen(string);

    while (original != string && *--original == junk)
        ;
    
    if (*original != '\0')
        original[*original == junk ? 0 : 1] = '\0';

    return string;
}
Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 53

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0701 seconds using 2.69MB