By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,190 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,190 IT Pros & Developers. It's quick & easy.

removing Spaces

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a

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

P: n/a

"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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.