⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c74_21.htm

📁 经典c语言教程
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>strcat() 函数 </title>
<script language="javascript">
	
	function show(obj){
		obj.display='';
		
	}
	
</script>
</head>

<body bgcolor="#ccefcc">

<blockquote>
  <div align="center"><center><table border="6" width="322" cellspacing="0" cellpadding="6" height="150" bordercolor="#FF9933">
    <tr>
      <th width="546" bgcolor="#FF9933">程序</th>
    </tr>
    <tr>
      <td ALIGN="center" width="546" bgcolor="#00FFFF"><p align="left">#include &lt;stdio.h&gt;<br>
      main()<br>
      {<br>
      &nbsp; static char name[80];<br>
      &nbsp; static char addon[]=&quot;? Good name!&quot;;<br>
      &nbsp; puts(&quot;what is your name?&quot;);<br>
      &nbsp; gets(name);<br>
      &nbsp; strcat(name, addon);<br>
      &nbsp; puts(name);<br>
      &nbsp; puts(addon);<br>
      }</td>
    </tr>
  </table>
  </center></div><p>程序测试:<input type="button" value="开始" name="B1" onClick="show(d1)"></p>
  <div id="div1" style="display:'none'"><form name="form1">
    <p>what is your name?<input type="text" name="input1" size="20"><input type="button" value="Enter" name="B2" onClick="var s=document.form1.input1.value+'? Good name!? Good name!';var v=document.form2.output1; v.value=s;show(d2)"></p>
  </form>
  </div><div id="div2" style="display:'none'"><form name="form2">
    <p><input type="text" name="output1" size="40"></p>
  </form>
  </div><script language="javascript">
<!--
	var d1=div1.style;
	var d2=div2.style;
-->
</script>
<p>正如你所看到的, strcat() (串连接)将需要的两个串作为参数, 
  第二个串的一个拷贝已经连在第一个串的尾部而第二个串没有任何改变。<br>
  </p>
  <p>注意上面的函数不检查第一个数组中是否能有足够空间放入第二个串。如果你在头一个数组中申请足够空间失败, 
  你还会遇到一些问题。<br>
  <br>
  当然, 你也可以在使用之前先使用 strlen() 函数看一下是否可以放入。<br>
  <a href="javascript:show(p1)">请看完善的程序</a>。<img src="../../img/lefthand.gif" alt="lefthand.jpg (983 bytes)" WIDTH="45" HEIGHT="20"></p>
  <div id="p1div" style="display='none'"><div align="center"><center><table border="6" width="406" cellspacing="0" cellpadding="6" height="150" bordercolor="#FF9933">
    <tr>
      <th width="630" bgcolor="#FF9933">程序</th>
    </tr>
    <tr>
      <td ALIGN="center" width="630" bgcolor="#00FFFF"><p align="left">#include &lt;stdio.h&gt;<br>
      #define SIZE 80<br>
      main()<br>
      {<br>
      &nbsp; static char name[SIZE];<br>
      &nbsp; static char addon[]=&quot;? Good name!&quot;;<br>
      &nbsp; puts(&quot;What is your name?&quot;);<br>
      &nbsp; gets(name);<br>
      &nbsp; if ((strlen(addon) + strlen(name) + 1) &lt; SIZE)<br>
      &nbsp;&nbsp;&nbsp; strcat(name,addon);<br>
      &nbsp; puts(name);<br>
      }</td>
    </tr>
  </table>
  </center></div></div><p>我们在合并后的长度上又加了一个1, 
  这个空间留给空字符。<br>
  如果你的 C 库中没有 strcat() 函数, 你自己可以很容易地写一个。<br>
  <a href="javascript:show(p2)">请看示例</a>。<img src="../../img/lefthand.gif" alt="lefthand.jpg (983 bytes)" WIDTH="45" HEIGHT="20"><br>
  </p>
  <div id="p2div" style="display:'none'"><div align="center"><center><table border="6" width="322" cellspacing="0" cellpadding="6" height="150" bordercolor="#FF9933">
    <tr>
      <th width="546" bgcolor="#FF9933">程序</th>
    </tr>
    <tr>
      <td ALIGN="center" width="546" bgcolor="#00FFFF"><p align="left">str_cat()<br>
      void str_cat(char str1[],*str2)<br>
      {<br>
      &nbsp; int count = 0;<br>
      &nbsp; while (str1[count] != '\0')<br>
      &nbsp;&nbsp;&nbsp; count++;<br>
      &nbsp; while (*str2 != '\0')<br>
      &nbsp;&nbsp;&nbsp; str1[count++] = *str2++;<br>
      &nbsp; str1[count] = '\0';<br>
      }</td>
    </tr>
  </table>
  </center></div></div><script language="javascript">
<!--
	var p1=p1div.style;
	var p2=p2div.style;
-->
</script>

</blockquote>

<p align="center"><a href="javascript:close()">关闭</a></p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -