修剪空白 [英] Trimming whitespaces
问题描述
有一些c代码可以取一个字符串(可能是也可能不是
在字符串之前或之后有空格),即东西,并且修剪掉
之前和之后的空白。
代码:
char * trim(char * str ,char ch)
{
char * first,* last;
int count;
/ *首先移动到与ch * /
不同的第一个字符(first = str; * first == ch; first ++);
/ *最后移动到空字符。这是了解100%我们
的唯一方法是
*从字符串末尾删除项目* /
for(last = first ; * last!=''\''';最后++);
/ *现在我们回溯直到找到一个与
$ b $不同的字符b ch * /
for(last - ; * last == ch; last - );
if(first!= str)
{
for(count = 0; count< last - first + 1; count ++)
str [count] = *(first + count) ;
str [count] =''\ 0'';
}
其他
{
str [last-first] =''\ 0'';
}
返回str;
}
问题是它总是删除str的最后一个字母。
ie
" ;东西 - > " STUF"任何想法为什么会这样。
干杯
John
have a bit of c code that is ment to take a string (that may or may not
have spaces before or after the string) i.e. " stuff ", and trims off
the whitespace before and after.
Code:
char *trim (char *str, char ch)
{
char *first, *last;
int count;
/* Move first to the first character that isn''t the same as ch */
for (first = str; *first == ch; first++);
/* Move last to the null character. Thats the only way to know 100% we
are
* removing items from the end of the string */
for (last = first; *last != ''\0''; last++);
/* Ok now we backtrack until we find a character that isn''t the same as
ch */
for (last--; *last == ch; last--);
if ( first != str)
{
for (count=0; count< last - first + 1; count++)
str[count] = *(first+count);
str[count] = ''\0'';
}
else
{
str[last-first] = ''\0'';
}
return str;
}
the problem is that it always removes the last letter of str as well.
i.e.
" stuff " -> "stuf" any ideas why this is happening.
Cheers
John
推荐答案
jo******@hotmail.com 写道:
有一些c代码,用于取一个字符串
(可能会或可能不会在字符串之前或之后有空格),即东西,并修剪掉之前和之后的空白。
代码:
char * trim(char * str,char ch)
{
char * first,* last;
int count;
/ *首先移动到第一个与ch * /
不同的字符(first = str; * first == ch; first ++);
该行可以在(ch ==''\''')时运行一个数组。
/ *好了现在我们回溯直到找到
与ch * /
不同的字符(last--; * last == ch; last - );
have a bit of c code that is ment to take a string
(that may or may not
have spaces before or after the string) i.e. " stuff ", and trims off
the whitespace before and after.
Code:
char *trim (char *str, char ch)
{
char *first, *last;
int count;
/* Move first to the first character that isn''t the same as ch */
for (first = str; *first == ch; first++);
That line could over run an array when (ch == ''\0'').
/* Ok now we backtrack until we find a character that
isn''t the same as ch */
for (last--; *last == ch; last--);
当一个字符串由一个空终止的
字符数组组成,这些字符都等于ch时,会发生什么?
-
pete
When a string consists of a null terminated array of
characters which are all equal to ch, then what happens?
--
pete
pete写道:
jo ****** @ hotmail.com 写道:
有一些c代码,需要一个字符串
(可能会或可能不会在字符串之前或之后有空格)
即东西,并修剪掉之前和之后的空白。
代码:
char * trim(char * str,char ch)
{
char * first,* last;
int count;
/ *首先移动到第一个与ch * /
不同的字符(first = str; * first == ch; first ++);
have a bit of c code that is ment to take a string
(that may or may not
have spaces before or after the string)
i.e. " stuff ", and trims off
the whitespace before and after.
Code:
char *trim (char *str, char ch)
{
char *first, *last;
int count;
/* Move first to the first character that isn''t the same as ch */
for (first = str; *first == ch; first++);
当(ch ==''\''')时,该行可能会运行一个数组。
That line could over run an array when (ch == ''\0'').
/ *现在我们回溯直到找到一个与ch * /
不同的字符(last--; * last == ch; last- - );
/* Ok now we backtrack until we find a character that
isn''t the same as ch */
for (last--; *last == ch; last--);
当一个字符串由一个空终止的
字符数组组成,这些字符都等于ch时,会发生什么?
When a string consists of a null terminated array of
characters which are all equal to ch, then what happens?
#include< string.h>
char * trim(char * str,char ch)
{
char * const p = str;
while(* str!=''\ 0''&& * str == ch){
++ str;
}
memmove(p,str,1 + st rlen(str));
str = p + strlen(p);
while(str!= p&& * - str == ch){
* str =''\ 0'';
}
返回p;
}
-
pete
#include <string.h>
char *trim(char *str, char ch)
{
char *const p = str;
while (*str != ''\0'' && *str == ch) {
++str;
}
memmove(p, str, 1 + strlen(str));
str = p + strlen(p);
while (str != p && *--str == ch) {
*str = ''\0'';
}
return p;
}
--
pete
John,>
你是否将const char *作为参数传递给trim函数?
即让你在main()中说你调用trim(stuff,'''' '');
如果你像这样传递const char *你不能对
str []下标进行任何更改,因为这是一个只读部分哪个你不能
更改。
如果你必须在trim中传递一个参数,它必须是一个数组
地址或分配指针。
John,
Are you passing const char* as an argument to the trim function?
i.e. let''s say in main() are you invoking trim(" stuff ", '' '');
If you are passing const char* like this you can''t do any changes in
str[] subscript because this is a read-only section which you can''t
change.
If you have to pass an argument in trim , it has to be either an array
address or allocated pointer.
这篇关于修剪空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!