首页 > 编程知识 正文

字符串比较函数strcmp,strstr函数的用法例子

时间:2023-05-05 21:18:30 阅读:156568 作者:2032

函数原型:

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 .

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。