在C中如何strcmp仅开头2个字符然后连接?
在C中如何strcmp仅开头2个字符?然后与另一个字符串连接?像这样的东西:
char s[10];
scanf("%s",s);
/* if i input "cs332" or "cs234", anything start with cs */
if (strcmp("cs",???)==0)
strcat(s,"by professor");
In C how do I strcmp just the beginning 2 characters? Then concatenate with another string? Something like this:
char s[10];
scanf("%s",s);
/* if i input "cs332" or "cs234", anything start with cs */
if (strcmp("cs",???)==0)
strcat(s,"by professor");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用strncmp
Use
strncmp
您正在寻找
strncmp
函数,该函数在功能上与strcmp
相同,但限制检查的字符数。因此,您将使用长度为 2 的比较字符串和"cs"
。但是,这里还有一些其他问题。首先,你的缓冲区不够大。当您将文本“by Professor”附加到十个字符的缓冲区时,没有任何字符串适合该缓冲区。
其次,健壮的代码永远不会将
scanf
与无界字符串格式说明符一起使用:这会导致缓冲区溢出问题。scanf
系列适用于格式化输入,并且未格式化比用户输入多一点:-)如果您想要一个强大的输入解决方案,请参阅 我之前的答案之一。
第三,您应该始终假设连接字符串可能会溢出缓冲区,并引入代码来防止这种情况发生。您需要添加:
并确保缓冲区足够大。
我将使用的方法是拥有一个(例如)200 字节的缓冲区,使用链接答案中的 getLine() (在下面复制以使该答案独立)且大小足够小(比如 100),那么您可以放心,附加“by Professor”不会溢出缓冲区。
功能:
测试代码:
You are looking for the
strncmp
function which is functionally identical tostrcmp
but limits the number of characters checked. So you would use it with a length of two and the comparison string of"cs"
. But, you have a few other problems here.First, your buffer is not big enough. There is no string that will fit into a ten-character buffer when you append the text "by professor" to it.
Secondly, robust code will never use
scanf
with an unbounded-string format specifier: that's asking for a buffer overflow problem. Thescanf
family is meant for formatted input and there is little more unformatted than user input :-)If you want a robust input solution, see one of my previous answers.
Thirdly, you should always assume that concatenating a string may overflow your buffer, and introduce code to prevent this. You need to add up:
and ensure the buffer is big enough.
The method I would use would be to have a (for example) 200-byte buffer, use
getLine()
from the linked answer (reproduced below to make this answer self-contained) with a sufficiently smaller size (say 100), then you can be assured that appending "by professor" will not overflow the buffer.Function:
Test code:
为什么不直接比较字符而不是调用strcmp?
例如
if(s[0]=='c' && s[1]=='s'){
...
}
why not directly comparing characters rather than calling strcmp?
E.g.
if(s[0]=='c' && s[1]=='s'){
...
}
有几种方法可以做到这一点。
字符串比较:
这是一种简单的方法,因为您无法轻松地将其扩展到稍长的代码(例如长度为 3/4 个字符)。
我想您已经知道应该使用
strncmp()
对吗?字符串连接
不要使用 strcat。真的。如果您连接两个长度大于
s
(目标)大小的字符串,您就会遇到麻烦。考虑使用snprint()
代替,例如这个:或者,您可以使用
strncat()
正如 Heath 指出的:Several ways to do this.
String comparison:
Is the naive way, as you can't expand this easily to slightly longer codes (say 3/4 characters in length).
I guess you've gathered you should be using
strncmp()
right?String Concaternation
Don't use strcat. Really. If you concatenate two strings whose length is greater than the size of
s
(the destination) you're in trouble. Consider usingsnprint()
instead, like this:Or, you could use
strncat()
as Heath points out:您可以使用 strncmp。
编辑:
You can use strncmp.
Edit:
是的,如上所述,strcmp 是首选方法。这里只是用不同的方法来做同样的事情。
Yes, as said, strcmp is the preferred method. Here's just a different way to do the same.