📄 c74_31.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>strcmp() 函数</title>
<script language="javascript">
function init(){
ex=exdiv.style;
p1=p1div.style;
p2=p2div.style;
out=outdiv.style;
}
function show(obj){
obj.display='';
}
</script>
<link rel="stylesheet" href="../../cstyle.css" type="text/scc">
</head>
<body bgcolor="#ccefcc" onLoad="init()">
<blockquote>
<p>想一想, 下边这个程序能正确执行吗? 这样做会奏效吗? </p>
<div align="center"><center><table border="6" width="374" cellspacing="0" cellpadding="6" height="150" bordercolor="#FF9933">
<tr>
<th width="598" bgcolor="#FF9933">程序</th>
</tr>
<tr>
<td ALIGN="center" width="598" bgcolor="#00FFFF"><p align="left">#include <stdio.h><br>
#define ANSWER "Grant"<br>
main()<br>
{<br>
char try[40];<br>
puts("Who is buried in Grant's tomb?");<br>
gets(try);<br>
while (try != ANSWER)<br>
{<br>
puts("No, that's wrong. Try again.");<br>
gets(try);<br>
}<br>
puts("That's right!");<br>
}</td>
</tr>
</table>
</center></div><p>考虑好了吗? 我们<a href="javascript:show(ex)">看一下解释</a>吧!<br>
</p>
<div id="exdiv" style="display:'none'"><p>它不会正确地执行。try 和 ANSWER
实际上都是指针, 因此 try!=ANSWER 不是判断两个串是否相同,
而是判断由 try 和 ANSWER 指向的地址是否一样。因为 try 和 ANSWER
是存储在不同的位置, 这两个指针永远不会相同,
同样用户也会一直被告知自己是错误的。<br>
</p>
<p>我们需要的是比较串的内容的函数,
而不是比较串的地址的函数。我们可以设计一个函数,
但是这项工作已由 strcmp (串比较)做了。<br>
我们现在看一下<a href="javascript:show(p1)">修改好的程序</a>: </p>
<div id="p1div" style="display:'none'">
<dd><div align="center"><center><table border="6" width="374" cellspacing="0" cellpadding="6" height="150" bordercolor="#FF9933">
<tr>
<th width="598" bgcolor="#FF9933">修改好的程序</th>
</tr>
<tr>
<td ALIGN="center" width="598" bgcolor="#00FFFF"><p align="left">#include <stdio.h><br>
#define ANSWER "Grant"<br>
main()<br>
{<br>
char try[40];<br>
puts("Who is buried in Grant's tomb?");<br>
gets(try);<br>
while (strcmp(try,ANSWER) != 0)<br>
{<br>
puts("No, that's wrong. Try again.");<br>
gets(try);<br>
}<br>
puts("That's right!");<br>
}</td>
</tr>
</table>
</center></div><p>从这个例子可以看出, strcmp()
用两个串指针作参数并且如果两串相同的话返回 0。<br>
strcmp() 的优点之一是它比较的是串而不是数组, 因此,尽管数组try占用了
40 个内存单元而Grant 只占6 个, 比较仍然只进行到try
中的头一个空字符停止。</p>
<p class="note">记住: strcmp() 可以用来比较放在不同长度数组中的串。</p>
<p>顺便问一句,在串不相同时strcmp() 返回什么值呢? 这里有一个<a href="javascript:show(p2)">例子</a>: </p>
<div id="p2div" style="display:'none'"><div align="center"><center><table border="6" width="374" cellspacing="0" cellpadding="6" height="150" bordercolor="#FF9933">
<tr>
<th width="598" bgcolor="#FF9933">修改好的程序</th>
</tr>
<tr>
<td ALIGN="center" width="598" bgcolor="#00FFFF"><p align="left">#include <stdio.h><br>
main()<br>
{<br>
printf("%d\n",strcmp("A","A"));<br>
printf("%d\n",strcmp("A","B"));<br>
printf("%d\n",strcmp("B","A"));<br>
printf("%d\n",strcmp("C","A"));<br>
printf("%d\n",strcmp("apples","apple"));<br>
}</td>
</tr>
</table>
</center></div><p>想一下输出是什么? <a href="javascript:show(out)">请看输出</a></p>
<div id="outdiv" style="display:'none'"><p>0<br>
-1<br>
1<br>
2<br>
115</p>
</div><p>从这个例子可以清楚地看出: strcmp()一直比到它发现头一对不相同字符为止,
然后返回这两个字符的 ASCII 码的差值`。<br>
<br>
例如, 在最后一个例子中, "apple" 直到它的 's' 之前还是和
"apple" 一样的, 这种比较一直进行到 "apple" 的第 6
个字符, 也就是 '\0', ASCII 0, 返回值是: 's'-'\0'=115-0=115。<br>
<br>
当然, 如果我们的 C 库函数中没有 strcmp() 函数,
我们可以很容易地写出一个来, 这个任务留给你自己去做。 </p>
</div></dd>
</div></div>
</blockquote>
<p align="center"><a href="javascript:close()">关闭</a></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -