473,544 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

removing Spaces

Hi,

Everytime I received a fix-length of string, let say 15 (the unused portion
will filled with Spaces before receive), I want to remove the Spaces from
END until I encounter a non-space char.
let say: testBuf is the buffer that receive this fix-length string.

printf("[%s]",testBuf);
Output:
[Hello World ]

How to remove the spaces to become [Hello World] ? Any fast and effcient C
algorithm like using strchr,etc?

Thanks.
Nov 14 '05 #1
12 3461

-----Original Message-----
From: Magix [mailto:ma***@as ia.com]
Posted At: 09 September 2004 10:25
Posted To: c
Conversation: removing Spaces
Subject: removing Spaces
Hi,

Everytime I received a fix-length of string, let say 15 (the
unused portion
will filled with Spaces before receive), I want to remove the
Spaces from
END until I encounter a non-space char.
let say: testBuf is the buffer that receive this fix-length string.

printf("[%s]",testBuf);
Output:
[Hello World ]

How to remove the spaces to become [Hello World] ? Any fast
and effcient C
algorithm like using strchr,etc?

Thanks.


This should get you started. But you will need to include some
error checking though.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main (void)
{
char testBuff[]="Hello World! ";

int len ;

len = strlen(testBuff )-1;

while(isspace(t estBuff[len]))
len--;
testBuff[len]='\0';

printf("Buffer = [%s]\n",testBuff) ;

return 0;
}

GST

Nov 14 '05 #2

"Magix" <ma***@asia.com > wrote in message
news:41******** **@news.tm.net. my...
Hi,

Everytime I received a fix-length of string, let say 15 (the unused portion will filled with Spaces before receive), I want to remove the Spaces from
END until I encounter a non-space char.
let say: testBuf is the buffer that receive this fix-length string.

printf("[%s]",testBuf);
Output:
[Hello World ]

How to remove the spaces to become [Hello World] ? Any fast and effcient C
algorithm like using strchr,etc?

Thanks.

I have something like this, but any better way ?

void str_delete(char * sourc, int pos, int len)
{
int i, j;

--pos;
j=strlen(sourc) ;
i=pos+len;
while (i <= j)
{
sourc[pos]=sourc[i];
++i;
++pos;
}
}

int isNonSpaceFound =0;
for (i=(strlen(test Buf)-1);i>0;i--)
{
if (testBuf[i]!=0x20)
{
isNonSpaceFound =1;
}
if (isNonSpaceFoun d==0)
{
str_delete(test Buf,strlen(test Buf),1);
}
}
Nov 14 '05 #3
"Turner, GS (Geoff)" wrote:
From: Magix [mailto:ma***@as ia.com]

Everytime I received a fix-length of string, let say 15 (the
unused portion will filled with Spaces before receive), I want
to remove the Spaces from END until I encounter a non-space
char. let say: testBuf is the buffer that receive this << fix-length string.
printf("[%s]",testBuf);
Output:
[Hello World ]

How to remove the spaces to become [Hello World] ? Any fast
and effcient C algorithm like using strchr,etc?


This should get you started. But you will need to include some
error checking though.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main (void)
{
char testBuff[]="Hello World! ";
int len ;

len = strlen(testBuff )-1;
** while(isspace(t estBuff[len]))
** len--;
** testBuff[len]='\0';
printf("Buffer = [%s]\n",testBuff) ;
return 0;
}


Won't work like that. Replace the lines marked with ** with:

while (len && (' ' == testBuff[len]))
testBuff[len--] = '\0';

--
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"We have always known that heedless self-interest was bad
morals. We now know that it is bad economics" - FDR
Nov 14 '05 #4
"Magix" <ma***@asia.com > wrote in message
news:41******** **@news.tm.net. my...
Hi,

Everytime I received a fix-length of string, let say 15 (the unused
portion
will filled with Spaces before receive), I want to remove the Spaces from
END until I encounter a non-space char.
let say: testBuf is the buffer that receive this fix-length string.

printf("[%s]",testBuf);
Output:
[Hello World ]

How to remove the spaces to become [Hello World] ? Any fast and effcient C
algorithm like using strchr,etc?


You've had a couple of examples, but they both use strlen(). Using strlen
means you're stepping forward over the string once to find its length, then
in the examples you've been given, you're stepping backwards over the string
to find the first non-space character. While this works fine, you can do the
entire operation by just stepping forward over the string once like this:

void str_trim (char *s) {
char *p;
char tog=0;
p=s;

while (*s != '\0') {
if (*s == ' ' && !tog) {
tog=1;
p=s;
} else if (*s != ' ') {
tog=0;
}
++s;
}
if (tog) {
*p='\0';
}
}

This may be quicker than the other methods preposed - it may be slower as
there are more equality tests, but I doubt it makes a great deal of
difference either way.
~Kieran Simkin
Digital Crocus
http://digital-crocus.com/
Nov 14 '05 #5
On Thu, 9 Sep 2004 11:40:23 +0100
"Turner, GS (Geoff) " <G.********@rl. ac.uk> wrote:
Return-Receipt-To: "Turner, GS (Geoff) " <G.********@rl. ac.uk>
Do you really want receipt notification from everyone who reads this
group?

<snip>
Everytime I received a fix-length of string, let say 15 (the
unused portion
will filled with Spaces before receive), I want to remove the
Spaces from
END until I encounter a non-space char.
let say: testBuf is the buffer that receive this fix-length string.

printf("[%s]",testBuf);
Output:
[Hello World ]

How to remove the spaces to become [Hello World] ? Any fast
and effcient C
algorithm like using strchr,etc?

Thanks.


This should get you started. But you will need to include some
error checking though.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main (void)
{
char testBuff[]="Hello World! ";


Please don't use tabs to indent on posts to Usenet. There has been a
discussion about this recently here in comp.lang.c
int len ;

len = strlen(testBuff )-1;

while(isspace(t estBuff[len]))
This doesn't only check for the space character, so it might not be what
the OP wants.
len--;
testBuff[len]='\0';
Here you eradicate the last non-space character.
printf("Buffer = [%s]\n",testBuff) ;

return 0;
}


Here is a slightly edited version of your program.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main (void)
{
char testBuff[]="Hello World! ";
int len ;

len = strlen(testBuff );

while(isspace(t estBuff[--len]))
/* do nothing */;
len++;

testBuff[len]='\0';
printf("Buffer = [%s]\n",testBuff) ;

return 0;
}
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #6

-----Original Message-----
From: Flash Gordon [mailto:sp**@fla sh-gordon.me.uk]
Posted At: 09 September 2004 13:03
Posted To: c
Conversation: removing Spaces
Subject: Re: removing Spaces
On Thu, 9 Sep 2004 11:40:23 +0100
"Turner, GS (Geoff) " <G.********@rl. ac.uk> wrote:
Return-Receipt-To: "Turner, GS (Geoff) " <G.********@rl. ac.uk>


Do you really want receipt notification from everyone who reads this
group?

<snip>

<snip>
Have I turned it off now?

Nov 14 '05 #7
Magix wrote:
"Magix" <ma***@asia.com > wrote in message
news:41******** **@news.tm.net. my...
Hi,

Everytime I received a fix-length of string, let say 15 (the unused


portion
will filled with Spaces before receive), I want to remove the Spaces from
END until I encounter a non-space char.
let say: testBuf is the buffer that receive this fix-length string.

printf("[%s]",testBuf);
Output:
[Hello World ]

How to remove the spaces to become [Hello World] ? Any fast and effcient C
algorithm like using strchr,etc?

Thanks.


I have something like this, but any better way ?

void str_delete(char * sourc, int pos, int len)
{
int i, j;

--pos;
j=strlen(sourc) ;
i=pos+len;
while (i <= j)
{
sourc[pos]=sourc[i];
++i;
++pos;
}
}

int isNonSpaceFound =0;
for (i=(strlen(test Buf)-1);i>0;i--)
{
if (testBuf[i]!=0x20)
{
isNonSpaceFound =1;
}
if (isNonSpaceFoun d==0)
{
str_delete(test Buf,strlen(test Buf),1);
}
}

Yes, there are better methods. So far, I haven't seen
anything that skips the space between "Hello" and "World"
and deletes the remaining spaces.

One idea: Extract the characters between '[' and ']'
as a string, then trim all the spaces off the right end.

Here is the second idea:
void Special_Trim(ch ar * text)
{
char * p_rt_bracket = strchr(text, ']');
if (!p_rt_bracket)
{
char * p_non_space = p_rt_bracket;

/* Point to first character left of ']' */
--p_non_space;

/* Point to first non-space character that */
/* is to the left of the ']'. */
while ( (p_non_space != text)
&& (*p_non_space == ' '))
{
--p_non_space;
}

/* Copy from the ']' and beyond over the */
/* space. */
++p_non_space;
while (*p_rt_bracket)
{
*p_non_space++ = *p_rt_bracket++ ;
}
*p_non_space = *p_rt_bracket;
}
return;
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #8
On Thu, 9 Sep 2004 16:15:03 +0100
"Turner, GS (Geoff) " <G.********@rl. ac.uk> wrote:
From: Flash Gordon [mailto:sp**@fla sh-gordon.me.uk]

On Thu, 9 Sep 2004 11:40:23 +0100
"Turner, GS (Geoff) " <G.********@rl. ac.uk> wrote:
Return-Receipt-To: "Turner, GS (Geoff) " <G.********@rl. ac.uk>


Do you really want receipt notification from everyone who reads this
group?

<snip>

<snip>
Have I turned it off now?


Yes, that's better.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #9
Kieran Simkin wrote:
"Magix" <ma***@asia.com > wrote in message
news:41******** **@news.tm.net. my...
Hi,

Everytime I received a fix-length of string, let say 15 (the unused
portion
will filled with Spaces before receive), I want to remove the Spaces from
END until I encounter a non-space char.
let say: testBuf is the buffer that receive this fix-length string.

printf("[%s]",testBuf);
Output:
[Hello World ]

How to remove the spaces to become [Hello World] ? Any fast and effcient C
algorithm like using strchr,etc?

You've had a couple of examples, but they both use strlen(). Using strlen
means you're stepping forward over the string once to find its length, then
in the examples you've been given, you're stepping backwards over the string
to find the first non-space character. While this works fine, you can do the
entire operation by just stepping forward over the string once like this:

void str_trim (char *s) {
char *p;
char tog=0;
p=s;

while (*s != '\0') {
if (*s == ' ' && !tog) {
tog=1;
p=s;
} else if (*s != ' ') {
tog=0;
}
++s;
}
if (tog) {
*p='\0';
}
}

This may be quicker than the other methods preposed - it may be slower as
there are more equality tests, but I doubt it makes a great deal of
difference either way.
~Kieran Simkin
Digital Crocus
http://digital-crocus.com/


char * rtrim(char *str) {
char *s, *p; int c;
s = p = str;
while ((c = *s++)) if (c != ' ') p = s;
*p = '\0';
return str;
}

--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6723
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test " would be renamed "test" (the 2 spaces before and after the filename removed). Any suggestions on how to do this? import os, re, string print...
1
10826
by: Andy Visniewski | last post by:
I have three columns, RecordID, FirstName, and LastName, but somehow through some program glitch, there is sometimes a trailing space in the firstname and lastname columns, for example, a persons name could be entered as "John " "Smith" or "Bob " "Johnson " I know there is a RTRIM function in sql, but the problem I/m having is making an...
8
14264
by: Peter O'Reilly | last post by:
I have an HTML form with a textarea input box. When the user conducts a post request (e.g. clicks the submit button), an HTML preview page is presented to them with the information they have filled out in the prior page's form elements. Naturally some users like to copy and paste text into the textarea box and presumably do so from say a...
11
14980
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white spaces(if it more than once between two words). Is there any function we have in C which will find out the tabs and white spaces and returns the...
1
1783
by: Mike in Paradise | last post by:
Is there a more effcient way of removing the spaces from the names for a Enumerated value that has several values when you split it)??? When you do a toString it puts ,<SPACE> between the entries eg. category.ToString() = "cat1, cat2, cat3" What I am currently doing..(and I use this a lot..) this.categoriesEntry.Items.AddRange(
9
10510
by: David Pratt | last post by:
Hi. I'm trying to clean files for packaging on mac using os.path.walk and want to clean the .DS_Store files that are hidden from view but could end up in code that I produce. # Clean mac .DS_Store if current_file == '.DS_Store': print 'a DS_Store item encountered' os.remove(f) My code walks the folders properly and print prints the...
2
6786
by: warthogweb | last post by:
This is in relation to BANFA's succinct reply to my post the other day. I have a bunch of phone numbers that contain one or two spaces and that I want to get consistent by removing them. However, UPDATE berg SET phone="033" WHERE phone="033 "; doesn't seem to remove them. Any ideas?
11
3039
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove spaces, but not in the inner string(\"and a inner string\\\"). Will anyone please tell me how to do this?
2
10345
code green
by: code green | last post by:
I am trying to write a simple function that will take a string containing an address line or business name and return it nicely formatted. By this I mean extra spaces removed and words capitalised. I also wish it to be legal XML.. The result string is further sent to another function to check the names and addresses don't appear on a...
0
7414
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...
0
7359
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7757
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5895
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...
1
5288
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4905
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...
1
1833
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
1
982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
651
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.