📄 c74.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>字符串函数 </title>
<script language="javascript">
var prePage="http://www.nec.sjtu.edu.cn/support/Course/C/c/c7/c/c7/c73.htm";
var nextPage="c/c7/c75.htm";
function show(obj){
if(obj.visibility=="hidden")
obj.visibility="visible";
else obj.visibility="hidden";
}
function showwin(url,winname,properties){
window.open(url,winname,properties)
}
</script>
<link rel="stylesheet" href="../cstyle.css" type="text/css">
<bgsound src="../voice/c74.au" loop="1">
</head>
<body background="../img/mainback.jpg" bgproperties="fixed">
<h2 align="center"><font face="楷体_GB2312"><a name="_top"></a>7.4 字符串函数</font></h2>
<table border="0" width="100%">
<tr>
<td width="20%" align="center"><a href="c74.htm#c741.html#c741">strlen() 函数</a></td>
<td width="20%" align="center"><a href="c74.htm#c742.html#c742">strcat() 函数</a></td>
<td width="20%" align="center"><a href="c74.htm#c743.html#c743">strcmp() 函数</a></td>
<td width="20%" align="center"><a href="c74.htm#c744.html#c744">strcpy() 函数</a></td>
<td width="20%" align="center"><a href="c74.htm#c745.html#c745">练 习 题</a></td>
</tr>
</table>
<hr>
<h3><a name="c741"></a>1.strlen() 函数</h3>
<blockquote>
<p>首先, 让我们写一个计算字符串中字符个数的函数,
我们将调用函数 str_len() 并且让它以字符串为参数。<br>
<br>
str_len()<br>
int str_len(char string[])<br>
{<br>
int count = 0;<br>
while (string[count] != '\0')<br>
++count;<br>
return(count);<br>
}<br>
<br>
此函数计算数组中的字符的个数并且将这个值返回给调用它的函数。字符的个数不包括表示字符串结尾的空字符。<br>
<br>
这儿有一个使用它的例子: <br>
main()<br>
{<br>
static char word1[] = "aster";<br>
static char word2[] = "at";<br>
static char word3[] = "awe";<br>
printf("%d %d %d\n",
str_len(word1),str_len(word2),str_len(word3));<br>
}<br>
<br>
<img src="../img/righthand.gif" alt="righthand.jpg (973 bytes)" WIDTH="45" HEIGHT="20"><a
href="javascript:d=output1.style;show(d)">输出</a></p>
<div id="output1" style="visibility:'hidden'"><p>5 2 3</p>
</div><p>C 库函数中的 strlen() 与 str_len() 函数有相同作用,
我们将在下一个例子中使用它,
那是一个将过长的串缩短的一个函数。<br>
<br>
fit(char *string, int size)<br>
{<br>
if (strlen(string) > size)<br>
*(string + size) = '\0';<br>
}<br>
main()<br>
{<br>
static char mesg[] = "Hold on to your hat";<br>
puts(mesg);<br>
fit(mesg,10);<br>
puts(mesg);<br>
}<br>
</p>
<p><img src="../img/righthand.gif" alt="righthand.jpg (973 bytes)" WIDTH="45"
HEIGHT="20"><a href="javascript:d=output2.style;show(d)">输出</a></p>
<div id="output2" style="visibility:hidden"><p>Hold on to your hat<br>
Hold on to</p>
</div><p>我们的函数在数组的第十一个元素放了一个 '\0' 字符,
数组的剩余部分仍然没变, 但 puts 在碰到头一个空字符时停止,
并且忽略数组的剩余部分。<br>
</p>
<p align="right"><a href="c74.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<hr>
<h3><a name="c742"></a>2.strcat() 函数</h3>
<blockquote>
<p><img src="../img/righthand.gif" alt="righthand.jpg (973 bytes)" WIDTH="45"
HEIGHT="20"><a
href="javascript:showwin('c74_21.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=700,height=600')">这里</a>说明了
strcat() 可以做什么。</p>
<p align="right"><a href="c74.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<hr>
<h3><a name="c743"></a>3.strcmp() 函数</h3>
<blockquote>
<p><img src="../img/righthand.gif" alt="righthand.jpg (973 bytes)" WIDTH="45"
HEIGHT="20"><a
href="javascript:showwin('c74_31.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=700,height=600')">这里</a>说明了
strcmp() 可以做什么。</p>
<p align="right"><a href="c74.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<hr>
<h3><a name="c744"></a>4.strcpy() 函数</h3>
<blockquote>
<p><img src="../img/righthand.gif" alt="righthand.jpg (973 bytes)" WIDTH="45"
HEIGHT="20"><a
href="javascript:showwin('c74_41.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=700,height=600')">这里</a>说明了
strcpy() 可以做什么。</p>
<p align="right"><a href="c74.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<hr>
<h3><a name="c745"></a>5.练习题</h3>
<blockquote>
<p><img src="../img/righthand.gif" alt="righthand.jpg (973 bytes)" WIDTH="45"
HEIGHT="20"><a
href="javascript:showwin('c74_51.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=700,height=600')">练习一</a><br>
<br>
<br>
<img src="../img/righthand.gif" alt="righthand.jpg (973 bytes)" WIDTH="45" HEIGHT="20"><a
href="javascript:showwin('c74_52.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=700,height=600')">练习二</a><br>
</p>
<p align="right"><a href="c74.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<p align="center"><a href="http://www.nec.sjtu.edu.cn/support/Course/C/c/c7/c75.htm"><img src="../img/next.gif" width="145" height="30"
alt="next.gif (3633 bytes)" border="0"></a></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -