【C语言】模拟实现strcmp()、strncmp()函数
来源:互联网 发布:linux系统的手机 编辑:程序博客网 时间:2024/05/02 02:11
//strcmp
函数原型:int strcmp(const *string1,const char *string2);
参数说明:string1为第一个要比较的字符串,string2为第二个要比较的字符串
函数功能:比较字符串str1和str2的大小。
返回说明:返回整数值:当str1<str2时,返回值<0;
当str1=str2时,返回值=0;
当str1>str2时,返回值>0。
#pragma warning (disable:4996)#include<stdio.h>#include<windows.h>#include<assert.h>//#include<string.h>int my_strcmp(const char *str1, const char *str2) //区分字符大小写{ assert(str1);assert(str2);while (*str1&&*str2&&(*str1==*str2)){str1++;str2++;}return *str1 - *str2;}int my__stricmp(char *str1,char *str2) //不区分字符大小写{ assert(str1);assert(str2);while (*str1&&*str2 ){if (*str1>= 'A'&&*str1 <= 'Z')*str1= *str1 + 'a' - 'A';if (*str2 >= 'A'&&*str2 <= 'Z')*str2 = *str2 + 'a' - 'A';if (*str1 != *str2){break;}str1++;str2++;}return *str1 - *str2;}int main(){char string1[] = "The quick brown dog jumps over the lazy fox";char string2[] = "The QUICK brown dog jumps over the lazy fox";char tmp[20];int result;/* 区分字符大小写 */printf("Compare strings:\n\t%s\n\t%s\n\n", string1, string2);result = my_strcmp(string1, string2);if (result > 0)strcpy(tmp, ">");else if (result < 0)strcpy(tmp, "<");elsestrcpy(tmp, "==");printf("\tstrcmp: String 1 %s string 2\n", tmp);/* 不区分字符大小写( my_stricmp) */result = my__stricmp(string1, string2);if (result > 0)strcpy(tmp, ">");else if (result < 0)strcpy(tmp, "<");elsestrcpy(tmp, "==");printf("\t_stricmp: String 1 %s string 2\n", tmp);system("pause");return 0;}
//strncmp
函数原型:int strncmp (const char *string1,const char *string2,size_count)
参数说明:string1为第一个要比较的字符串,string2为第二个要比较的字符串,count为指定的str1与str2的比较的字符数。
函数功能:比较字符串str1和str2的前n个字符。
返回说明:返回整数值:当str1<str2时,返回值<0;
当str1=str2时,返回值=0;
当str1>str2时,返回值>0。
#pragma warning (disable:4996)#include<stdio.h>#include<windows.h>#include<assert.h>int my_strncmp(const char *str1, const char *str2, size_t count){assert(str1);assert(str2);while (*str1&&*str2&&(*str1 == *str2)&&count--){str1++;str2++;}return *str1 - *str2;}int my__strnicmp(char *str1, char *str2, size_t count){assert(str1);assert(str2);while (*str1&&*str2&&count--){if (*str1 >= 'A'&&*str1 <= 'Z')*str1 = *str1 + 'a' - 'A';if (*str2 >= 'A'&&*str2 <= 'Z')*str2 = *str2 + 'a' - 'A';if (*str1 != *str2)break;str1++;str2++;}return *str1 - *str2;}int main(){char string1[] = "The quick brown dog jumps over the lazy fox";char string2[] = "The QUICK brown fox jumps over the lazy dog";char tmp[20];int result;printf("Compare strings:\n\t\t%s\n\t\t%s\n\n", string1, string2);printf("strncmp is case sensitive\n");result = my_strncmp(string1, string2, 10); //区分字符大小写if (result > 0)strcpy(tmp, ">");else if (result < 0)strcpy(tmp, "<");elsestrcpy(tmp, "==");printf("Result:\t\tString 1 %s string 2\n\n", tmp);printf("_strnicmp is case-insensitive\n"); //不区分字符的大小写result = my__strnicmp(string1, string2, 10); if (result > 0)strcpy(tmp, ">");else if (result < 0)strcpy(tmp, "<");elsestrcpy(tmp, "==");printf("Result:\t\tString 1 %s string 2\n\n", tmp);system("pause");return 0;}
0 0
- 【C语言】模拟实现strcmp()、strncmp()函数
- C语言 模拟实现strcmp strncmp
- 模拟实现strcmp strncmp函数
- 【C语言】模拟实现strcmp函数
- 【c语言】模拟实现strcmp函数
- 【面试题】C语言:模拟实现memcmp,试比较memcmp与strcmp,strncmp的区别
- [C语言]模拟实现strcpy/strncpy/strcat/strncat/strcmp/strncmp/strstr
- 模拟实现C语言strncpy.strncat.strncmp函数
- 【C语言】模拟实现strncpy、strncat、strncmp函数
- 【C语言】模拟实现strncpy strncat strncmp strchr strrchr函数
- C语言~模拟实现strncpy、strncat、strncmp函数
- 用C语言 实现strcmp(),strncmp()相同功能的myStrcmp()函数和myStrncmp()函数
- 模拟实现strcpy,strcmp,strstr,strncpy,strncmp,strcat,strlen函数
- 模拟实现strlen、strcpy、strcmp、strncmp等字符串函数
- 实现strcmp( )和strncmp( )函数
- 利用c语言函数调用写strcmp,strncmp
- 【笔试题】C语言:模拟实现strncmp
- 【C语言】模拟实现strcpy、strcat、strcmp函数
- 系统架构设计师教程知识整理——绪论(一)
- 英语学术论文常用句型
- 区块链专家技能图谱
- XUtils—支持大文件上传,更全面的网络工具
- deepin linux mysql工具
- 【C语言】模拟实现strcmp()、strncmp()函数
- 倍增法求LCA的模板
- 【Untiy&NGUI】判断鼠标坐标位置与实际屏幕坐标的比较
- 基于64位win10系统离线配置Android Studio
- IQKeyBoardManager按钮"Done"能否改为"完成",能的话怎么改?
- [00803]让对象支持上下文管理协议
- 学徒的时候安卓老知识点总结汇总 (新手应该大概了解的)
- Note06
- iOS网络