函数原型:
strcmp(constchar*S1、const char *s2 ); 用法比较:
s1s2时,返回负数;
s1==s2时,返回值=0;
对于s1s2,返回正数。
也就是说,从左向右一个字符一个字符地比较两个字符串。 用ASCII值的大小进行比较。 比较直到出现不同的字符或遇到“ 0”。
比较输入字符串顺序的用法:
int main () {using namespace std; int n=0; char input[80]; cout'enterwords(tostop,type the word done ) :' endl; while(cininput(if ) strcmp (input,' done ' ) ) /比较输入字符串和' done '字符串的位置顺序。 {n; }elsebreak; } cout ' youenteredatotalof ' n ' words.' endl; 如果输入a b c d e f g done for sure
程序结果:
You entered a total of 7 words .