removing Spaces

M

Magix

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

Turner, GS (Geoff)

-----Original Message-----
From: Magix [mailto:[email protected]]
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
 
M

Magix

Magix said:
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;
++pos;
}
}

int isNonSpaceFound=0;
for (i=(strlen(testBuf)-1);i>0;i--)
{
if (testBuf!=0x20)
{
isNonSpaceFound =1;
}
if (isNonSpaceFound==0)
{
str_delete(testBuf,strlen(testBuf),1);
}
}
 
F

Flash Gordon

Return-Receipt-To: "Turner, GS (Geoff) " <[email protected]>

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

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;
}
 
C

CBFalconer

Turner said:
From: Magix [mailto:[email protected]]

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';
 
K

Kieran Simkin

Magix said:
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/
 
T

Thomas Matthews

Magix said:
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;
++pos;
}
}

int isNonSpaceFound=0;
for (i=(strlen(testBuf)-1);i>0;i--)
{
if (testBuf!=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
 
J

Joe Wright

Kieran said:
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;
}
 
C

CBFalconer

Joe said:
.... 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.
 
P

Paul

Magix said:
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.
 
M

Magix

CBFalconer said:
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.
 
G

guggach

hi
two ways:
the barbarian one, set a char pointer at end
of string, come back until black-char.

Code:
--------------------

char *ppp = other_str+strlen(other_str);
for(--ppp; ppp-other_str >0 && isspace(*ppp); --ppp); /* end for */
*ppp = 0;
printf("|%s|\n",other_str);

--------------------

----
a civilized one (costs more):
(assuming we can|must sqeeze spaces to one)

Code:
--------------------

char *ppp, *qqq;
register int blanc = 0;

for(ppp = qqq = other_str; *ppp; ++ppp){
if(isspace(*ppp)){ ++blanc; continue; }
if(blanc) *qqq++ = ' ';
*qqq++ = *ppp;
blanc = 0;
}
*qqq = 0;
printf("|%s|\n",other_str);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top