473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Simple TRIM function

Hi,

Here is an extension of MSDN's trim right example to trim left-right...
First subroutines then the calling structure is given below...

Cheers.

Ali Riza SARAL

//************************************************** ***********
char *trim_right( char *szSource )
{
char *pszEOS = 0;

// Set pointer to character before terminating NULL
pszEOS = szSource + strlen( szSource ) - 1;

// iterate backwards until non '_' is found
while( (pszEOS >= szSource) && (*pszEOS == '_') )
*pszEOS-- = '\0';

return szSource;
}
char *trim_left( char *szSource )
{
char *pszBOS = 0;

// Set pointer to character before terminating NULL
//pszEOS = szSource + strlen( szSource ) - 1;
pszBOS = szSource;

// iterate backwards until non '_' is found
while(*pszBOS == '_')
*pszBOS++;

return pszBOS;
}
char *trim( char *szSource )
{
return trim_left(trim_right(trim_left(szSource)));
//************************************************** ********************
char *trim_right( char *szSource );
char *trim_left( char *szSource );
char *trim( char *szSource );
//...
int main(int argc, char *argv[])
{
//...
char szbuf[] = "__12345_____";

printf_s("\nBefore trim: %s\n\n", szbuf);
//printf_s("\nAfter trim right: %s\n", trim_right(szbuf));
//printf_s("\nAfter trim left: %s\n", trim_left(szbuf));
printf_s("\nAfter trim: %s\n", trim(szbuf));

Note: Once again I have built this on MSDN's simple trim example. I kept their var names etc but completed the gap they have left kindly.
Oct 31 '06 #1
1 20631
Hi,

Here is an extension of MSDN's trim right example to trim left-right...
First subroutines then the calling structure is given below...

Cheers.

Ali Riza SARAL

char *trim_left( char *szSource )
{
char *pszBOS = 0;

// Set pointer to character before terminating NULL
//pszEOS = szSource + strlen( szSource ) - 1;
pszBOS = szSource;

// iterate backwards until non '_' is found
while(*pszBOS == '_')
*pszBOS++;

return pszBOS;
}
Hi, i think that you have an error here.
You have to move the pszBOS pointer only, not the memory destination.

Replace these lines:
while(*pszBOS == '_')
*pszBOS++;

With these:
while(*pszBOS == '_')
pszBOS++;

Thanks anyway,
Damian Montaldo.
Apr 10 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
3
by: Andy B | last post by:
I've tried using Trim or RTrim to strip trailing space characters from my data. When I check on the transformed data space characters are still there. We have an address table containing two...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
7
by: Sascha Herpers | last post by:
Hi, what is the difference between the trim function and the trim String-member? As far as I see it, both return the trimmed string and leave the original string unaltered. Is any of the two...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
23
by: Rex | last post by:
Hi I want to write a procedure which takes in a string of names seperated by a whitespace and puts commas at each whitespace the last name however, should have "and" before it. Let me explain...
31
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char...
12
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?...
5
by: Marjeta | last post by:
I'm trying to very that the user actually entered something in the form, and not just spaces. I guess the problem is in the first line of isBlank() function. I've tried the following:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.