Chinaunix首页 | 论坛 | 博客
  • 博客访问: 238211
  • 博文数量: 35
  • 博客积分: 791
  • 博客等级: 军士长
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-05 16:56
文章分类
文章存档

2013年(7)

2012年(28)

我的朋友

分类: C/C++

2012-09-21 11:48:49

函数简介
原型:extern int strcmp(const char *s1,const char * s2);
用法:#include
功能:比较字符串s1和s2。
一般形式:strcmp(字符串1,字符串2)
程序实现:

点击(此处)折叠或打开

  1. #include <stdlib.h>
  2. #include <stdio.h>

  3. int main()
  4. {
  5.     int a;
  6.     char buf1[20]="hello";
  7.     char buf2[20]="hell";
  8.      a=strcmp(buf1,buf2);
  9.     if(a)
  10.     
  11.         printf("buf1 and buf2 is identity ");
  12.     else
  13.     
  14.         printf("buf1 and buf2 is not identity ");
  15.     return 0;

  16. }
  17. int strcmp(char *str1,char *str2)
  18. {
  19.     if(*str1=='\0'||*str2=='\0')
  20.         return 0;
  21.     while(*str1!='\0'&&*str2!='\0'&&*str1==*str2)
  22.     {
  23.         str1++;
  24.         str2++;

  25.         
  26.     }
  27.     if(*str1==*str2)
  28.         return 1;
  29.     else
  30.         return 0;

  31. }



阅读(5590) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~