删除空格 [英] removing Spaces

查看:74
本文介绍了删除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




每当我收到固定长度的字符串时,就说15(未使用的部分

将在收到前填充Spaces),我想从

END中删除空格,直到遇到非空格字符。

假设:testBuf是接收此固定长度字符串的缓冲区。 />

printf(" [%s]",testBuf);

输出:

[Hello World]


如何删除空格成为[Hello World]?任何快速有效的算法,如使用strchr等?


谢谢。

解决方案


-----原始消息-----
来自:Magix [mailto:ma *** @ asia.com] 发布到:c
会话:删除空格
主题:删除空格


<每当我收到固定长度的字符串时,就说15(
未使用的部分
将在收到前填充空格),我想从
空格> END,直到遇到非空格字符。
让我们说:testBuf是接收此固定长度字符串的缓冲区。

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

如何删除空格成为[Hello World]?任何使用strchr等快速且高效的算法?

谢谢。




这应该让你开始。但是你需要包含一些

错误检查。


#include< stdio.h>

#include< ; string.h>

#include< ctype.h>


int main(无效)

{

char testBuff [] =" Hello World! " ;;


int len;


len = strlen(testBuff)-1;


while(isspace(testBuff [len]))

len--;

testBuff [len] =''\ 0'';


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


返回0;

}


GST




" Magix" <毫安*** @ asia.com>在消息中写道

news:41 ********** @ news.tm.net.my ...


<每次我收到一个固定长度的字符串,比方说15(未使用的
部分将在收到前填充Spaces),我想从
END中移除空格直到遇到非space char。
让我们说:testBuf是接收这个固定长度字符串的缓冲区。

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

如何删除空格成为[Hello World]?任何快速有效的算法,如使用strchr等?

谢谢。



我有这样的东西,但是任何更好的方法?


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

}

}

" Turner,GS(Geoff)"写道:

来自:Magix [mailto:ma *** @ asia.com]

每当我收到固定长度的字符串,比方说15(
未使用的部分将在收到前填充Spaces),我希望
从END中删除空格,直到我遇到非空格的角色。比方说:testBuf是接收这个
<< fix-length string。
printf(" [%s]",testBuf);
输出:
[Hello World]

如何删除空格成为[Hello World]?任何快速且高效的C算法,如使用strchr等?



这应该可以让你入门。但是你需要包含一些
错误检查。

#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);
返回0 ;
}




不会那样工作。将标有**的行替换为:


while(len&&(''''== testBuff [len]))

testBuff [len--] =''\ 0'';


-

丘吉尔和布什都可以被视为战时领袖,只是<秘书处和埃德先生都是马匹。
。 - 詹姆斯罗德斯。

我们一直都知道,不顾一切的自我利益是坏的

道德。我们现在知道这是糟糕的经济学 - FDR


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.

解决方案


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



"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);
}
}


"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


这篇关于删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆