?? c74.htm
字號(hào):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>字符串函數(shù) </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 字符串函數(shù)</font></h2>
<table border="0" width="100%">
<tr>
<td width="20%" align="center"><a href="c74.htm#c741.html#c741">strlen() 函數(shù)</a></td>
<td width="20%" align="center"><a href="c74.htm#c742.html#c742">strcat() 函數(shù)</a></td>
<td width="20%" align="center"><a href="c74.htm#c743.html#c743">strcmp() 函數(shù)</a></td>
<td width="20%" align="center"><a href="c74.htm#c744.html#c744">strcpy() 函數(shù)</a></td>
<td width="20%" align="center"><a href="c74.htm#c745.html#c745">練 習(xí) 題</a></td>
</tr>
</table>
<hr>
<h3><a name="c741"></a>1.strlen() 函數(shù)</h3>
<blockquote>
<p>首先, 讓我們寫一個(gè)計(jì)算字符串中字符個(gè)數(shù)的函數(shù),
我們將調(diào)用函數(shù) str_len() 并且讓它以字符串為參數(shù)。<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>
此函數(shù)計(jì)算數(shù)組中的字符的個(gè)數(shù)并且將這個(gè)值返回給調(diào)用它的函數(shù)。字符的個(gè)數(shù)不包括表示字符串結(jié)尾的空字符。<br>
<br>
這兒有一個(gè)使用它的例子: <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 庫(kù)函數(shù)中的 strlen() 與 str_len() 函數(shù)有相同作用,
我們將在下一個(gè)例子中使用它,
那是一個(gè)將過(guò)長(zhǎng)的串縮短的一個(gè)函數(shù)。<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>我們的函數(shù)在數(shù)組的第十一個(gè)元素放了一個(gè) '\0' 字符,
數(shù)組的剩余部分仍然沒(méi)變, 但 puts 在碰到頭一個(gè)空字符時(shí)停止,
并且忽略數(shù)組的剩余部分。<br>
</p>
<p align="right"><a href="c74.htm#_top.html#_top">返回頁(yè)首</a></p>
</blockquote>
<hr>
<h3><a name="c742"></a>2.strcat() 函數(shù)</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>說(shuō)明了
strcat() 可以做什么。</p>
<p align="right"><a href="c74.htm#_top.html#_top">返回頁(yè)首</a></p>
</blockquote>
<hr>
<h3><a name="c743"></a>3.strcmp() 函數(shù)</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>說(shuō)明了
strcmp() 可以做什么。</p>
<p align="right"><a href="c74.htm#_top.html#_top">返回頁(yè)首</a></p>
</blockquote>
<hr>
<h3><a name="c744"></a>4.strcpy() 函數(shù)</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>說(shuō)明了
strcpy() 可以做什么。</p>
<p align="right"><a href="c74.htm#_top.html#_top">返回頁(yè)首</a></p>
</blockquote>
<hr>
<h3><a name="c745"></a>5.練習(xí)題</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')">練習(xí)一</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')">練習(xí)二</a><br>
</p>
<p align="right"><a href="c74.htm#_top.html#_top">返回頁(yè)首</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>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -