473,799 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple TRIM function

1 New Member
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("\nBef ore trim: %s\n\n", szbuf);
//printf_s("\nAft er trim right: %s\n", trim_right(szbu f));
//printf_s("\nAft er trim left: %s\n", trim_left(szbuf ));
printf_s("\nAft er 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 20672
dmontaldo
1 New Member
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
12767
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' or even why there could be a problem. here is the code ////////
3
10702
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 fields: BuildName and RoadName. Both have the following properties: size 50, not indexed, not required, allowed zero length. Some records have BuildName, RoadName as null, some have content. No content is 50 chr long. When i run a Len(BuildName)...
11
5359
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 (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
7
4225
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 faster? Is there a general rule/opinion to prefere members over functions? Thanks for any hint.
22
9756
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 thought I'd change it to the VB ..NET method "String.Trim" but that throws an object exception. Which brings the question: is it compliant to use Trim(String), or is it more within etiquette to use If Not String Is Nothing Then String.Trim?
23
1777
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 that with the help of an example: The original string is: "Toby Grant Michelle Tom" the procedure should return "Toby, Grant, Michelle and Tom" Cheers!
31
2926
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 *trim(char *s) { char *begin,*end; begin = s;
12
1400
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 3+) : String.prototype.lTrim = function() { return this.replace(/^\s+/,'');
5
3526
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: elem.value.trim(); elem.value=elem.value.trim(); elem.value=trim(elem.value); elem.value.replace(/^\s+|\s+$/g,"");and none works. It correctly gives me an error message if I leave the field completely blank.But if there's anything in the field, it does not...
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10490
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.