用 C 语言编写 String.trim()
我正在用 c 编写字符串修剪方法,这是我想出的代码。我认为它可以消除前导和尾随空格,但是,我希望代码可以更干净。您能提出改进建议吗?
void trim(char *String)
{
int i=0;j=0;
char c,lastc;
while(String[i])
{
c=String[i];
if(c!=' ')
{
String[j]=c;
j++;
}
else if(lastc!= ' ')
{
String[j]=c;
j++;
}
lastc = c;
i++;
}
这段代码看起来干净吗?
Possible Duplicates:
Painless way to trim leading/trailing whitespace in C?
Trim a string in C
I was writing the String trim method in c and this is the code I came up with. I think it does the job of eliminating leading and trailing whitespaces however, I wish the code could be cleaner. Can you suggest improvements?
void trim(char *String)
{
int i=0;j=0;
char c,lastc;
while(String[i])
{
c=String[i];
if(c!=' ')
{
String[j]=c;
j++;
}
else if(lastc!= ' ')
{
String[j]=c;
j++;
}
lastc = c;
i++;
}
Does this code look clean ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
看起来不干净。假设第一个字符是空格,则您将
lastc
与未定义的值一起使用。您将在末尾留下一个空格(如果末尾有空格,则按c
时将是一个空格,而lastc
则不会)。您也没有终止字符串。假设您解决了未初始化的
lastc
问题,您将把“abc”转换为“abcbc”,因为它在任何时候都没有被缩短。该代码还折叠了字符串内的多个空格。这不是你所描述的;这是期望的行为吗?
It doesn't look clean. Assuming the first character is a space, you're using
lastc
with an undefined value. You're leaving one space at the end (if there's a space at the end, when it's hitc
will be a space andlastc
won't).You're also not terminating the string. Assuming you fix the uninitialized
lastc
problem, you'll transform " abc" to "abcbc", since it's not being shortened at any point.The code also collapses multiple spaces inside the string. This isn't what you described; is it desired behavior?
如果您明智地使用标准库函数,它通常会使您的代码更具可读性 - 例如,
isspace()
和memmove()
在这里特别有用:It often makes your code more readable if you make judicious use of the standard library functions - for example,
isspace()
andmemmove()
are particularly useful here:该代码有几个问题。它仅检查空间。不是制表符或换行符。您正在复制字符串的整个非空白部分。并且您在设置之前使用了lastc。
这是一个替代版本(已编译但未测试):
There's several problems with that code. It only checks for space. Not tabs or newlines. You are copying the entire non-whitespace part of the string. And you are using lastc before setting it.
Here's an alternate version (compiled but not tested):
有一些问题:
lastc
可能在未初始化的情况下使用。例如,您可以使用 for 循环而不是 while 循环。此外,修剪/剥离功能通常取代空格、制表符和换行符。这是我不久前编写的使用指针的解决方案:
There are some problems:
lastc
could be used uninitialized. And you could make use of a for loop instead of a while loop, for example. Furthermore, trim/strip functions usually replace spaces, tabs and newlines.Here's a solution using pointers that I wrote quite a while ago:
这是我的解决方案。
简短、简单、干净、有注释且经过轻微测试。
它使用“isspace”分类功能,因此您可以轻松更改要修剪的“空白”的定义。
Here is my solution.
Short, simple, clean, commented, and lightly tested.
It uses the "isspace" classification function, so you can easily change your definition of "white space" to be trimmed.
我不会将字符与空格字符“ ”进行比较,而是使用“isspace”函数,我相信该函数是在 ctype.h 中定义的。
Instead of comparing a character with the space character ' ', I'd use the "isspace" function, which I believe is defined in ctype.h.
我不知道干净,但我发现很难遵循。如果我需要这样做,我最初会考虑分两个阶段:
然后,我可能会更仔细地研究您似乎试图实现的一次性解决方案,但前提是存在速度问题。
顺便说一句,您可能想要使用 isspace() 而不是仅检查空间。
I don't know about clean, but I find it hard to follow. If I needed to do this I'd initially think of it in two phases:
I might then look more closely at a one-pass solution like you seem to be trying to implement, but only if there was a speed problem.
By the way, you probably want to use
isspace()
rather than checking only for space.