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

📄 c71.htm

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

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>引言 </title>
<script language="javascript">
   var prePage="c/c6/c66.htm";
   var nextPage="c/c7/c72.htm";
</script>

<link rel="stylesheet" href="../cstyle.css" type="text/css">
<bgsound src="../voice/c71.au" loop="1">
</head>

<body background="../img/mainback.jpg" bgproperties="fixed">

<h2 align="center"><font face="楷体_GB2312"><a name="_top"></a>7.1 引言</font></h2>

<table border="0" width="100%">
  <tr>
    <td width="33%"><p align="center"><a href="c71.htm#c711.html#c711">字符串 </a></td>
    <td width="33%"><p align="center"><a href="c71.htm#c712.html#c712">字符数组和字符串</a></td>
    <td width="34%"><p align="center"><a href="c71.htm#c713.html#c713">例子</a></td>
  </tr>
</table>

<hr>

<h3><a name="c711"></a>1.字符串 </h3>

<blockquote>
  <p>在前言中, 我们写第一个 C 
  语言程序时就已经第一次介绍了字符串。在下面的语句中: <br>
  printf(&quot;C is a good language&quot;); 传递给函数 printf 
  的参数就是一个字符串。<br>
  <br>
  <font color="#FF0000">双引号</font>被用作字符串的边界符号, 
  其中可以包含除了<font color="#FF0000">双引号</font>之外的任何字母、数字以及特殊字符的任意组合。( 
  但是不久你将看到, 在字符串里包含一个<font color="#FF0000">双引号</font>, 
  也是完全可能的。)<br>
  <br>
  字符串是 C 语言中最有用而且最重要的数据结构之一。</p>
  <p class="note">要记住一个最基本的事实:字符串是一个以一个空字符 
  ('\0') 结尾的字符数组。</p>
  <p>在本章中, 我们将对字符串的特性进行学习:<ol>
    <li>如何说明和初始化一个字符串。</li>
    <li>如何在程序中对字符串进行输入和输出。</li>
    <li>如何使用字符串函数。</li>
    <li>如何使用字符串。</li>
  </ol>
  <p align="right"><a href="c71.htm#_top.html#_top">返回页首</a></p>
</blockquote>

<hr>

<h3><a name="c712"></a>2.字符数组和字符串</h3>

<blockquote>
  <p>在我们介绍 <font color="#FF0000">char</font> 数据类型的时候, 
  提到过这种类型的变量仅包含<font color="#FF0000">单个字符</font>。<br>
  为了赋一个单个字符给这样的变量, 字符是被括在一对<font
  color="#FF0000">单引号</font>中的。所以赋值语句:<br>
  <font color="#000080">plus_sign = '+';</font> 将把字符 '+' 赋给变量 plus_sign 。</p>
  <p class="note">注意: 单引号和双引号的用法是不同的。 在变量 plus_sign 
  为 char 类型的场合下, 语句:<br>
  plus_sign = &quot;+&quot;; 是非法的。</p>
  <p><img src="../img/c351.gif" alt="c351.jpg (3628 bytes)" align="right" WIDTH="248"
  HEIGHT="64">我们将如下定义一个名字为 word 的字符数组:<br>
  static char word[] = {'H','e','l','l','o','!'};<br>
  如右图所示那样, 这个语句在内存中占用了<font color="#FF0000">六</font>个字符空间。<br>
  <br>
  为了打印出数组 word 中的内容, 我们以 %c 的格式打印出 word 
  的各个元素。<br>
  <br>
  for (i=0; i&lt;6; ++i)<br>
  &nbsp;&nbsp;&nbsp; printf(&quot;%c&quot;,word[i]);<br>
  <br>
  输出: <font color="#008080"><strong>Hello!</strong></font><br>
  <br>
  不幸的是, 你将发现在程序中记住每个字符数组的<font color="#FF0000">字符数目</font>是一件令人厌烦的事情, 
  尤其在你想使用你的字符数组存贮<font color="#FF0000">变长</font>字符串时更是如此。<br>
  <br>
  在 C 语言中, 我们在每个字符串的<font color="#FF0000">尾部</font>放置一个特殊符号 
  <font color="#FF0000">'\0'</font>。用这种方式, 函数就能够<font color="#FF0000">自动</font>判断是否到达了字符串的尾部。<br>
  因此 static char word[] ={'H','e','l','l','o','!','\0'};定义了一个包含<font
  color="#FF0000">七</font>个字符的叫做 word 的字符数组, 
  它的最后一个字符是<font color="#FF0000">空字符</font>。<br>
  <br>
  所以, 我们就不必知道字符串 word 的<font color="#FF0000">长度</font>。<br>
  while (word[count] != '\0')<br>
  &nbsp;&nbsp;&nbsp; printf(&quot;%c&quot;,word[count++]);<br>
  <br>
  上面的语句与下面的语句是等价的。<br>
  static char word[] = {&quot;Hello!&quot;};<br>
  并且上面的 while 语句与下面的语句是等价。<br>
  printf(&quot;%s&quot;,word);I</p>
  <p>输出: <font color="#008080"><strong>Hello!</strong></font></p>
  <p>当然, 改写过的第二种语句更便于输出, 
  且可读性好。我们将在后面的课程中详细讨论这些情况。</p>
  <p align="right"><a href="c71.htm#_top.html#_top">返回页首</a></p>
</blockquote>

<hr>

<h3><a name="c713"></a>3.例子</h3>

<blockquote>
  <p>在第三章里, 我们介绍过合并两个字符串的函数。在那个例子里, 
  必须向该函数传递<font color="#FF0000">字符数组</font>和<font color="#FF0000">长度</font>这两个参数。这样, 
  才能把两个任意长度的字符数组合并在一起。<br>
  <br>
  但是, 使用<font color="#FF0000">字符串</font>, 
  我们就不必将数组长度传递给函数。这样我们就能够如下重写函数 
  concatenate:<br>
  concatenate ( char str1[], char str2[], char result[])<br>
  {<br>
  &nbsp;&nbsp;&nbsp; int i,j;<br>
  &nbsp;&nbsp;&nbsp; /* copy str1 to result. */<br>
  &nbsp;&nbsp;&nbsp; for (i=0; str1[i]!='\0'; ++i)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result[i] = str1[i];<br>
  &nbsp;&nbsp;&nbsp; /* copy str2 to result. */<br>
  &nbsp;&nbsp;&nbsp; for (j=0; str2[j]!='\0'; ++j)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result[i+j] = str2[j];<br>
  &nbsp;&nbsp;&nbsp; result[i+j] = '\0';<br>
  }<br>
  main()<br>
  { <br>
  &nbsp;&nbsp;&nbsp; char s1[] = {&quot;Test &quot;};<br>
  &nbsp;&nbsp;&nbsp; char s2[] = {&quot;works.&quot;};<br>
  &nbsp;&nbsp;&nbsp; char s3[20];<br>
  &nbsp;&nbsp;&nbsp; concatenate(s1,s2,s3);<br>
  }</p>
  <p>程序执行后 <font color="#FF0000">s3[]=={&quot;Test works.&quot;}</font><br>
  </p>
  <p align="right"><a href="c71.htm#_top.html#_top">返回页首</a></p>
</blockquote>

<p align="center"><a href="http://www.nec.sjtu.edu.cn/support/Course/C/c/c7/c72.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 + -