- 论坛徽章:
- 0
|
C++ Primer Plus第6版中文版 程序清单7.9 strgfun.cpp
----书上有这么一段话:“ 但字符串与常规char数组只是数组之间的一个重要区别是,字符串有内置的结束字符,而char数组只是数组不是字符串。这意味着不用将字符串的长度作为参数传递给函数,而函数可以使用循环依次检查字符串中的每个字符,直到遇到结尾的空值字符为止。”
然后附了下面这段例子,但我没的看出有什么区别,求教!!
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- unsigned int c_in_str(const char * str, char ch);
- int _tmain(int argc, _TCHAR* argv[])
- {
- char mmm[15] = "minimum"; // string in an array
- // some systems require preceding char with static to
- // enable array initialization
- char *wail = "ululate"; // wail points to string
- unsigned int ms = c_in_str(mmm, 'm');
- unsigned int us = c_in_str(wail, 'u');
- cout << ms << " m characters in " << mmm << endl;
- cout << us << " u characters in " << wail << endl;
- cin.get();
- return 0;
- }
- // this function counts the number of ch characters
- // in the string str
- unsigned int c_in_str(const char * str, char ch)
- {
- unsigned int count = 0;
- while (*str) // quit when *str is '\0'
- {
- if (*str == ch)
- count++;
- str++; // move pointer to next char
- cout <<*str<<endl;
- }
-
- return count;
- }
复制代码 |
|