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

These are standard little tools C programmers use:

#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

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&gt;0\)\) s.erase\(0,fs\);
\}

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

int main\(\)
\{
	string test; test=" X";
	cout&lt;&lt;"before :"&lt;&lt;test&lt;&lt;": ";
	ltrim\(test\);
	cout&lt;&lt;"After ltrim :"&lt;&lt;test&lt;&lt;": ";
	rpad\(test, 32, ' '\);
	cout&lt;&lt;"after rpad  :"&lt;&lt;test&lt;&lt;":"&lt;&lt;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();
}