C语言教程:strcmp()函数

2018-10-0708:12:08编程语言入门到精通Comments3,241 views字数 508阅读模式

strcmp(first_string, second_string)函数比较两个字符串,如果两个字符串相等,则返回0文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6443.html

在这里,我们使用gets()函数从控制台读取两个字符串用来做比较。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6443.html

使用示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6443.html

创建一个源文件:string_strcmp.c,其代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6443.html

#include <stdio.h>  
void main()
{
    char str1[20], str2[20];

    while(1){
        printf("Enter 1st string: ");
        gets(str1);//reads string from console  
        printf("Enter 2nd string: ");
        gets(str2);
        if (strcmp(str1, str2) == 0)
            printf("Strings are equal \n");
        else
            printf("Strings are not equal \n");
    }
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6443.html

Enter 1st string: mystr
Enter 2nd string: mystr
Strings are equal
Enter 1st string: string1
Enter 2nd string: string2
Strings are not equal
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6443.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/6443.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定