The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
pointer problem useless79 High Level Programming 1 11-07-2007 10:24 PM
problem with if -z string Terrible UNIX for Dummies Questions & Answers 1 06-01-2006 04:01 PM
sed problem - replacement string should be same length as matching string. amangeles Shell Programming and Scripting 4 01-11-2006 02:11 AM
Problem with function which reutrns pointer to a value jazz High Level Programming 5 10-28-2005 07:03 AM
sed/awk String problem odogbolu98 UNIX for Dummies Questions & Answers 2 05-03-2002 09:59 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-30-2008
Registered User
 

Join Date: Oct 2004
Posts: 215
String and pointer problem

i am having a string like

" X1 " ---> string lenght is 30

I have stored this to a chararry . ref[30]

so here ref = " X1 "

now i trim the left space by my function . Si the string now becomes

"X1 " ---> string lenght is 15

I want this string as 30 character array , when i tried to print after trining the space it is becoming 15 in length

After triming on the 30 character array , i need to add the balnk space in the end .

in short after i do a trim the string should be as

"X1 " --- correct
"X1 " --> incorrect now after triming i am getting like i am not able to achieve the above

can you please help me to do this achieved

Thanks,
Arun
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-30-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,493
These are standard little tools C programmers use:
Code:
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

char *ltrim(char *dest)
{
	if(*dest)
	{
		char *working=calloc(1,strlen(dest)+1);
		char *p=dest;
		while(isspace(*p)) p++;
		strcpy(working,p);
		strcpy(dest, working);
		free(working);
	}
	return dest;
}

char *rpad(char *dest, size_t len, int padchar)
{
    if(strlen(dest) < len)
    {
		char *working=calloc(1, len+1);
	    memset(working, padchar, len);
	    memcpy(working, dest, strlen(dest));
	    strcpy(dest, working);
		free(working);
	}
	return dest;
}

int main()
{
	char test[64]={0x0};
	strcpy(test, " X");
	printf("before :%s: ", test);
	ltrim(test);
	printf("After ltrim :%s: ", test);
	rpad(test, 32, ' ');
	printf("after rpad  :%s:\n", test);
	return 0;
}
Reply With Quote
  #3 (permalink)  
Old 07-02-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 500
Quote:
Originally Posted by arunkumar_mca View Post
now i trim the left space by my function . Si the string now becomes

"X1 " ---> string lenght is 15

I want this string as 30 character array , when i tried to print after trining the space it is becoming 15 in length
Can you show the source of the trim function
Reply With Quote
  #4 (permalink)  
Old 07-02-2008
Registered User
 

Join Date: Dec 2007
Location: Simi Valley, CA
Posts: 28
OO / STL approach

Here's another way to do it - I barely tested it, but it looks like it works.

namespace cpp_stl_method
{
using namespace std;

#define whitespaces ((char*)" \f\v\r\n\t")
inline void ltrim(string& s,char* ws=whitespaces)
{
int fs=s.find_first_not_of(ws);
if ((fs!=string::npos) && (fs>0)) s.erase(0,fs);
}

inline void rpad(string&s, int n, char ch)
{
int l=n-s.size();
if (l>0) s.append(l,ch);
}

int main()
{
string test; test=" X";
cout<<"before :"<<test<<": ";
ltrim(test);
cout<<"After ltrim :"<<test<<": ";
rpad(test, 32, ' ');
cout<<"after rpad :"<<test<<":"<<endl;
return 0;
}
} // cpp_stl_method


int main()
{
// Cmethod::main(); // you can try the C method here and compare the results
cpp_stl_method::main();
}
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0