473,320 Members | 1,810 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,320 software developers and data experts.

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 3446

-----Original Message-----
From: Magix [mailto:ma***@asia.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(testBuff[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(testBuf)-1);i>0;i--)
{
if (testBuf[i]!=0x20)
{
isNonSpaceFound =1;
}
if (isNonSpaceFound==0)
{
str_delete(testBuf,strlen(testBuf),1);
}
}
Nov 14 '05 #3
"Turner, GS (Geoff)" wrote:
From: Magix [mailto:ma***@asia.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(testBuff[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(testBuff[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(testBuff[--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**@flash-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(testBuf)-1);i>0;i--)
{
if (testBuf[i]!=0x20)
{
isNonSpaceFound =1;
}
if (isNonSpaceFound==0)
{
str_delete(testBuf,strlen(testBuf),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(char * 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.learn.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**@flash-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
Joe Wright wrote:
.... snip ...
char * rtrim(char *str) {
char *s, *p; int c;
s = p = str;
while ((c = *s++)) if (c != ' ') p = s;
*p = '\0';
return str;
}


That has the usage trap (discussed in another thread) of returning
the same string. Better IMO is this slight modification:

size_t rtrim(char *s)
{
char *s, *p, c;

s = p = str;
while ((c = *s++))
if (' ' != c) p = s;
*p = '\0';
return p - str;
}

i.e. having done the work to scan over the string, return the
length which may be useful and can avoid a relatively expensive
future call to strlen.

--
"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 #11
"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.


void TrimSpace(char *pStr)
{
char* pLoc = pStr;
while(*pStr)
if(*pStr++ != ' ')
pLoc = pStr;
*pLoc = '\0';
}

int main(void)
{
char yw[] = "You are welcome! ";
TrimSpace(yw);
return 0;
}

-Paul.
Nov 14 '05 #12

"CBFalconer" <cb********@yahoo.com> wrote in message
news:41***************@yahoo.com...
Joe Wright wrote:

... snip ...

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


That has the usage trap (discussed in another thread) of returning
the same string. Better IMO is this slight modification:

size_t rtrim(char *s)
{
char *s, *p, c;

s = p = str;
while ((c = *s++))
if (' ' != c) p = s;
*p = '\0';
return p - str;
}

i.e. having done the work to scan over the string, return the
length which may be useful and can avoid a relatively expensive
future call to strlen.

--
"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


Thanks to all for your tips.
Nov 14 '05 #13

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

Similar topics

9
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 ...
1
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...
8
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...
11
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...
1
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...
9
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...
2
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,...
11
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...
2
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....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.