Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

How to trim the white space around a string in C program

Programming


Tags
awk, awk trim, trim, trim awk

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-31-2005
hxm1303 hxm1303 is offline
Registered User
 
Join Date: Aug 2005
Last Activity: 31 August 2005, 7:36 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
How to trim the white space around a string in C program

I am coding a C program to read a plain text file. There are a lot of blank fields or a string with white spaces. I want to know is there such a function called trim() in C to clean the white space around a string? Or some other way can do this efficiently? Thanks.
Sponsored Links
    #2  
Old Unix and Linux 09-01-2005
RishiPahuja's Unix or Linux Image
RishiPahuja RishiPahuja is offline
Registered User
 
Join Date: Apr 2005
Last Activity: 15 April 2011, 1:11 AM EDT
Location: Bangalore, India
Posts: 203
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL

writing your own will be as efficient as provided Unix or Linux Image

char *trim(char *str)
{
char ptr[strlen(str)+1];
int i,j=0;
for(i=0;str[i]!='\0';i++)
{
if (str[i] != ' ' && str[i] != '\t')
ptr[j++]=str[i];
}
ptr[j]='\0'
str=ptr;
}

though written C code after long long time, and its not tested one too... but it left me in good mood.

happy programming!!! Unix or Linux Image


Rishi
Sponsored Links
    #3  
Old Unix and Linux 09-06-2005
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 9 April 2015, 4:19 AM EDT
Location: classification algos
Posts: 3,206
Thanks: 19
Thanked 30 Times in 27 Posts
Hi

this is only a part,

strip leading and trailing spaces; check for specified patterns

#define SUCCESS 1
#define FAILURE 0

// 0-for space and 1-for digit

// you can have allowable patterns in Patterns Array.


char *Patterns[]={"010", "1", "10", "01"};
char *b=" 1 23456";
char *temp;
int i, j=0, len, prev=-1;
len=strlen(b);
temp=(char*)calloc(10, sizeof(char));
for ( i=0; i<len; i++ )
{
if( b[i] != ' ' )
{
if( prev != 1 )
temp[j++]='1';
}
else
{
if( prev != 0 )
temp[j++]='0';
}
prev = ( b[i] == ' ' ) ? 0 : 1;
}
temp[j]='\0';
for ( i=0; i<=3; i++ )
{
if( strcmp(Patterns[i], temp) == 0 )
{
len=strlen(b);
for ( prev=0, j=0; j<len; j++ )
{
if ( b[j] != ' ' )
temp[prev++]=b[j];
}
temp[prev]='\0';
break;
}
}
    #4  
Old Unix and Linux 09-24-2008
rmh rmh is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 16 September 2009, 3:31 PM EDT
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
yeah, this is a years' old thread; here's a generic pattern that can be used:


Code:
/*----------trim (char) c from right-side of string *p------------------*/
char *strtrim_right( register char *p, register c)
{
    register char *end;
    register int len;

    len = strlen( p);
    while ( *p && len)
    {
        end = p + len-1;
        if( c == *end)
            *end = 0;
        else
            break;
        len = strlen( p);
    }
    return( p);
}

strtrim_left() is left as an exercise to the reader, but you can use the principle here quite readily.
The Following User Says Thank You to rmh For This Useful Post:
pygmalion666 (09-16-2012)
Sponsored Links
    #5  
Old Unix and Linux 10-08-2008
rmh rmh is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 16 September 2009, 3:31 PM EDT
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
I should have mentioned that whitespace is considered to be any of the following characters:

0x09 - horizontal tab
0x0a - linefeed
0x0b - vertical tab
0x0c - form feed
0x0d - carriage return
0x20 - space

so keep that in mind when you program for trimming whitespace...
Sponsored Links
    #6  
Old Unix and Linux 10-08-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 9 April 2015, 4:19 AM EDT
Location: classification algos
Posts: 3,206
Thanks: 19
Thanked 30 Times in 27 Posts

Code:
register char *end;
    register int len;

I don't see a specific reason to see register variables here, which is not guaranteed as well.
Sponsored Links
    #7  
Old Unix and Linux 10-09-2008
rmh rmh is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 16 September 2009, 3:31 PM EDT
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
You do realize that the register class hints that the declared objects will be accessed frequently, right.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed + white space mcclunyboy Shell Programming and Scripting 18 10-01-2010 03:23 AM
Add white space to the end of a line with sed karlanderson Shell Programming and Scripting 7 03-09-2009 06:20 AM
SED with White Space sandeep_1105 UNIX for Dummies Questions & Answers 1 03-02-2009 03:48 PM
Trim white spaces using awk mona Shell Programming and Scripting 2 07-28-2006 04:47 AM
How to trim space in output variable ? vj_76 Shell Programming and Scripting 5 06-12-2005 03:03 AM



All times are GMT -4. The time now is 07:34 PM.