![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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;
}
|
|
|||
|
Can you show the source of the trim function
|
|
|||
|
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(); } |
|||
| Google UNIX.COM |
| Thread Tools | |
| Display Modes | |
|
|