一、简述
在头文件<cstring>
中,有一个函数strcmp()
。
二、详细介绍
strcmp()
比较字符串。格式为 strcmp(const char *Str1, const char *Str2)
,由此可见,其接受两个 const char * 参数。
例如:
int num = strcmp(FINISHED,words);
—→其作用是比较 FINISHED 数组中的字符串
和 words 数组中的字符串
,并返回一个值。
① 若两个字符串相同,则返回 0;
② 若第一个字符串按字母顺序排在第二个字符串之前,则返回一个负数;
③ 若第一个字符串按字母顺序排在第二个字符串之后,则返回一个正数;
***注意:区分大小写。
三、题目
四、代码
#include<iostream>
#include<cstring>
using namespace std;
const int SIZE = 20;
const char FINISHED[] = "done";
int main()
{
char words[SIZE];
int counter = 0;
cout << "Enter words (to stop, type the word done):\n";
while (strcmp(FINISHED,words) != 0)
{
counter++;
cin >> words;
cin.get();
}
cout << "You enter a total of " << counter - 1 << " words.\n";
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)