String and pointer problem


 
Thread Tools Search this Thread
Top Forums Programming String and pointer problem
# 1  
Old 06-30-2008
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
# 2  
Old 06-30-2008
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;
}

# 3  
Old 07-02-2008
Quote:
Originally Posted by arunkumar_mca
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
# 4  
Old 07-03-2008
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();
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

String pointer does not work

Hello, I am trying to reverse complement one string and reverse another (NO complement!), both with pointer. My code compiled without error, but did not do the job I wanted. #include <stdio.h> #include <stdlib.h> #include <zlib.h> #include "kseq.h" // STEP 1: declare the type of file... (5 Replies)
Discussion started by: yifangt
5 Replies

2. Programming

Scanf() string pointer problem

I have a problem with scanf() for string pointer as member of a struct. #include <stdio.h> #include <stdlib.h> struct Student { int studentNumber; int phoneNumber; char *studentName; //line 7 // char studentName; //line 8 }; int... (10 Replies)
Discussion started by: yifangt
10 Replies

3. Programming

How i use pointer as a string in c programing?

I'm newbie learner. My all friend use windows just only me use linux. so i can't solve any problem by myself. i need a solution. how can i use pointer as a string. #include<string.h> #include<stdio.h> int main() { char *s='\0'; gets(s); puts(s); return 0; } This code work on... (6 Replies)
Discussion started by: raihan004
6 Replies

4. Programming

pointer problem

Does anyone know? int x = 1; int *p = &++x; //ok ! int *q = &x++; //gives an error :O why the first pointer is ok but the second is an error? (13 Replies)
Discussion started by: nishrestha
13 Replies

5. Programming

unidirectional linked list pointer problem

I am trying to test some operations on a directed list. However, the declaration of a pointer is giving me trouble. I seem to have done something incorrectly because I get an error: "listtest.c:29: warning: 'p' may be used uninitialized in this function" Can anyone help? This is my code... (6 Replies)
Discussion started by: bluetxxth
6 Replies

6. UNIX for Dummies Questions & Answers

Counting vowels in string. "Comparison pointer-integer".

I'm trying to write a programme which scans strings to find how many vowels they contain. I get an error saying that I'm trying to compare a pointer and an integer inif(*v == scanme){. How can I overcome this ? Also, the programme seems to scan only the first word of a string e.g.: if I type "abc... (1 Reply)
Discussion started by: fakuse
1 Replies

7. Programming

segfault in pointer to string program

hello all, my question is not about How code can be rewritten, i just wanna know even though i am not using read only memory of C (i have declared str) why this function gives me segfault :wall:and the other code executes comfortably though both code uses same pointer arithmetic. ... (4 Replies)
Discussion started by: zius_oram
4 Replies

8. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

9. Programming

pointer problem

could any one tell why the following is showing segmentation fault while using **ptr but working fine using **a #include<stdio.h> ... (1 Reply)
Discussion started by: useless79
1 Replies

10. Programming

Problem with function which reutrns pointer to a value

i have a function: char *pcCityIdToCountryName(ADMIN_DB_DATA *pstHEader, unit uiCityID) this returns a pointer to CountryName if cityId is given. to retrieve countryname i give: char *CountryName; CountryName = pcCityIdToCountryName(..................); but when i compile it is giving :... (5 Replies)
Discussion started by: jazz
5 Replies
Login or Register to Ask a Question