当前位置:三九宝宝网 → 宝宝百科 → 宝宝知识 → 正文

字符串比较函数strcmp是怎样比较出两个字符串的大小的

更新:03-18 整理:39baobao.com
字体:

[WinAPI字符及字符串函数1:CharLower字符或字符串转小写]unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Button1: TBu...+阅读

跟字符串的长度无关的.

就是从两个字符串的头开始比.相应位的字符进行对比.实际上是比较相应位字符的ASCII码大小.

如char *s1="abc";

char *s2="bd";

执行strcmp(s1,s2),其结果是负的.

从两个字符串的头开始比,因为字符串s1中的'a'小于s2中的'b'所以s1

再如:

char *s1="abc";

char *s2="ad";

其结果也是负的,因为s1中的'b'

再如:

char *s1="abc";

char *s2="ab";

其结果就是正的.因为s1中的'c'大于s2中的'\0';因为字符串的最后一个字符是\0;

再如:

char *s1="abc";

char *s2="abcd";

其结果是负的.因为s1中的'\0'小于s2中的'd';

用一个函数实现两个字符串的比较即自己写一个strcmp函数函数原

修改如下: #include void main() { int strcmp(char *s1,char *s2); char str1[30],str2[30]; printf("\n please input string1:"); gets(str1); printf("\n please input string2:"); gets(str2); printf("%d\n",strcmp(str1,str2)); } int strcmp(char *s1,char *s2) { int i=0; while (s1[i]&&s2[i]&&s1[i]==s2[i]) i++; return s1[i]-s2[i]; }

数据结构自己编写一个比较字符串大小的函数

int mystrcmp(char* str1, char* str2) { while(str1!= NULL & str2!= NULL) { if (*str1 != *str2 ) return *str1 - *str2; //遇到不等字符,返回差值,正表示str1字符串大于str2 else {str1++; str2++;} } if(str1 != NULL || str2!=NULL) //如果while循环已经比较到其中一个短字符串结束,则返回两字符串长度的差值,正表示str1字符串大于str2 return strlen(str1) - strlen(str2); return 0; //两字符串相等 }

本文地址:https://www.39baobao.com/show/39_351590.html

以上内容来自互联网,请自行判断内容的正确性。若本站收录的信息无意侵犯了贵司版权,请联系我们,我们会及时处理和回复,谢谢.

以下为关联文档:

WinAPI字符字符串函数3:CharUpper字符字符串转大写interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Button1: TButton;Button...

WinAPI字符字符串函数10:lstrcpy复制字符串interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Button1: TButton;Button...

WinAPI字符字符串函数9:lstrcat合并字符串interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button...

字符串比较函数修改如下 #include int strcmp(char *str1,char *str2) { for(;;str1++,str2++) { if(*str1==*str2) if(*str1=='\0') break; else continue; else break; } return *str1-*...

字符串比较函数strcp比较的是字符串的什么你是问strcmp函数么? strcmp函数是比较两个字符串的大小,返回比较的结果。一般形式是: strcmp(字符串1,字符串2); ①字符串1小于字符串2,strcmp函数返回一个负值; ②字符串1等于字...

一个字符串比较函数#include"stdio.h" #include"string.h" #include<iostream> using namespace std; void main() { char st[20]; char s[3][20]={"Turbo C","Visual C++","Hello World"}; int i,j,p; f...

写两个字符串比较是否相等的函数相等返回1不等返回0不能用库int compare(char *a, char *b) { while(*a || *b) { if( *a != *b) return 0; a++; b++; } return 1; } 这个函数没有用库函数,函数体内没有设一个变量,效率应该是非常高吧。...

sql字符串函数的问题新建一个函数,该函数实现类似于C#中String的split()函数。 函数定义如下: create function f_split(SourceStr varchar(MAX),StrSeprate varchar(10)) returns temp table(spli...

SQL判断字符串是否在目标字符串中的函数根据需求,写了一段方法。 用于识别以下的情况: 判断 字符串A 在用逗号分隔的字符串B中是否存在如: v_str_a = aa ; v_str_b= aa,bb,dd,cc ; 如上,就返回Y,否则返回N. 添加了一些校...