关于strcmp函数的小题
编写一个程序,它使用一个char数组和循环来每次读取一个单词,知道用户输入done为止,随后程序指出用户输入了多少个单词(不包括done在内)。下面是该程序的运行情况:
Enter:
anteater birthday
envy finagle done for sure
you entered a total of 4 words.
使用函数strcmp()来进行比较测试.
这个程序的该怎么写?希望说下原理 C++
[解决办法]
#include <iostream>
int main()
{
char cWord[20] = {0}; // 假设每个单词长度不超过19
UINT nCount = 0;
while (1)
{
scanf("%s" ,cWord); // 读取
if (0 == strcmp(cWord ,"done") ) // 判断是否"done",注意区分大小写
{
break;
}
nCount++; // 计数器自增
}
cout << "you entered a total of " << nCount << " words." << endl;
return 0;
}