strcmp()是如何工作的?
我一直在四处寻找答案。我将制作一系列自己的字符串函数,如strcmp()是如何工作的?,c,string,C,String,我一直在四处寻找答案。我将制作一系列自己的字符串函数,如my_strcmp(),my_strcat(),等等 strcmp()是否处理两个字符数组的每个索引,如果两个字符串的相同索引处的ASCII值较小,则该字符串按字母顺序较大,因此返回0或1或2?我想我问的是,它是否使用字符的ASCII值来返回这些结果 任何帮助都将不胜感激 [修订] 好的,所以我想出了这个。。。除第二个字符串大于第一个字符串外,它适用于所有情况 有什么建议吗 int my_strcmp(char s1[], char s2[
my_strcmp()
,my_strcat()
,等等
strcmp()
是否处理两个字符数组的每个索引,如果两个字符串的相同索引处的ASCII值较小,则该字符串按字母顺序较大,因此返回0或1或2?我想我问的是,它是否使用字符的ASCII值来返回这些结果
任何帮助都将不胜感激
[修订]
好的,所以我想出了这个。。。除第二个字符串大于第一个字符串外,它适用于所有情况
有什么建议吗
int my_strcmp(char s1[], char s2[])
{
int i = 0;
while ( s1[i] != '\0' )
{
if( s2[i] == '\0' ) { return 1; }
else if( s1[i] < s2[i] ) { return -1; }
else if( s1[i] > s2[i] ) { return 1; }
i++;
}
return 0;
}
int main (int argc, char *argv[])
{
int result = my_strcmp(argv[1], argv[2]);
printf("Value: %d \n", result);
return 0;
}
int my_strcmp(字符s1[],字符s2[]
{
int i=0;
而(s1[i]!='\0')
{
如果(s2[i]='\0'){return 1;}
else如果(s1[i]s2[i]){返回1;}
i++;
}
返回0;
}
int main(int argc,char*argv[])
{
int result=my_strcmp(argv[1],argv[2]);
printf(“值:%d\n”,结果);
返回0;
}
它使用字符的字节值,如果第一个字符串出现在第二个字符串之前(按字节值排序),则返回负值;如果相等,则返回零;如果第一个字符串出现在第二个字符串之后,则返回正值。由于它是在字节上操作的,所以它不知道编码
例如:
strcmp("abc", "def") < 0
strcmp("abc", "abcd") < 0 // null character is less than 'd'
strcmp("abc", "ABC") > 0 // 'a' > 'A' in ASCII
strcmp("abc", "abc") == 0
strcmp(“abc”、“def”)<0
strcmp(“abc”、“abcd”)<0//空字符小于“d”
strcmp(“abc”、“abc”)>0/'a'>ASCII格式的'a'
strcmp(“abc”、“abc”)==0
更准确地说,如中所述:
非零返回值的符号应由第一对字节(均被解释为类型unsigned char)值之间的差值符号确定,这两个字节值在所比较的字符串中不同
请注意,返回值可能不等于此差值,但它将带有相同的符号。以下是:
一旦两个字符之间存在不匹配,它只返回这两个字符之间的差异。strcmp的伪代码“实现”类似于:
define strcmp (s1, s2):
p1 = address of first character of str1
p2 = address of first character of str2
while contents of p1 not equal to null:
if contents of p2 equal to null:
return 1
if contents of p2 greater than contents of p1:
return -1
if contents of p1 greater than contents of p2:
return 1
advance p1
advance p2
if contents of p2 not equal to null:
return -1
return 0
基本上就是这样。依次比较每个字符,然后根据该字符决定第一个或第二个字符串是否更大
只有当字符相同时,才移动到下一个字符,如果所有字符都相同,则返回零
请注意,您可能不一定得到1和-1,规格说明任何正值或负值都足够,因此您应该始终使用<0
、>0
或==0
检查返回值
将其转换为真正的C将相对简单:
int myStrCmp (const char *s1, const char *s2) {
const unsigned char *p1 = (const unsigned char *)s1;
const unsigned char *p2 = (const unsigned char *)s2;
while (*p1 != '\0') {
if (*p2 == '\0') return 1;
if (*p2 > *p1) return -1;
if (*p1 > *p2) return 1;
p1++;
p2++;
}
if (*p2 != '\0') return -1;
return 0;
}
还要记住,字符上下文中的“更大”并不一定基于所有字符串函数的简单ASCII排序
C有一个称为“locales”的概念,它指定(除其他外)底层字符集的排序,例如,您可能会发现字符
a
、a
、a
和a
都被认为是相同的。这将发生在类似strcoll的函数中,这是我的版本,为小型微控制器应用程序编写,符合MISRA-C。
此代码的主要目的是编写可读代码,而不是编写大多数编译器lib中的一行goo
int8_t strcmp (const uint8_t* s1, const uint8_t* s2)
{
while ( (*s1 != '\0') && (*s1 == *s2) )
{
s1++;
s2++;
}
return (int8_t)( (int16_t)*s1 - (int16_t)*s2 );
}
注意:该代码假定为16位
int
类型。此代码等效、较短且可读性更强:
int8_t strcmp (const uint8_t* s1, const uint8_t* s2)
{
while( (*s1!='\0') && (*s1==*s2) ){
s1++;
s2++;
}
return (int8_t)*s1 - (int8_t)*s2;
}
我们只需要测试s1的末端,因为如果我们在s1末端之前到达s2末端,循环将终止(因为*s2!=*s1)
如果我们只使用7位(纯ASCII)字符,则返回表达式在每种情况下都会计算正确的值。由于存在整数溢出的风险,需要仔细考虑为8位字符生成正确的代码。这就是我如何实现strcmp: 它的工作原理如下: 它比较两个字符串的第一个字母,如果相同,则继续到下一个字母。如果不是,则返回相应的值。它非常简单易懂: #包括
//function declaration:
int strcmp(char string1[], char string2[]);
int main()
{
char string1[]=" The San Antonio spurs";
char string2[]=" will be champins again!";
//calling the function- strcmp
printf("\n number returned by the strcmp function: %d", strcmp(string1, string2));
getch();
return(0);
}
/**This function calculates the dictionary value of the string and compares it to another string.
it returns a number bigger than 0 if the first string is bigger than the second
it returns a number smaller than 0 if the second string is bigger than the first
input: string1, string2
output: value- can be 1, 0 or -1 according to the case*/
int strcmp(char string1[], char string2[])
{
int i=0;
int value=2; //this initialization value could be any number but the numbers that can be returned by the function
while(value==2)
{
if (string1[i]>string2[i])
{
value=1;
}
else if (string1[i]<string2[i])
{
value=-1;
}
else
{
i++;
}
}
return(value);
}
//函数声明:
int strcmp(字符串1[],字符串2[]);
int main()
{
char string1[]=“圣安东尼奥马刺队”;
char string2[]=“将再次成为champins!”;
//调用函数-strcmp
printf(“\n strcmp函数返回的编号:%d”,strcmp(string1,string2));
getch();
返回(0);
}
/**此函数计算字符串的字典值,并将其与另一个字符串进行比较。
如果第一个字符串大于第二个字符串,则返回一个大于0的数字
如果第二个字符串大于第一个字符串,则返回一个小于0的数字
输入:string1、string2
输出:值-根据情况可以是1、0或-1*/
int strcmp(字符字符串1[],字符字符串2[]
{
int i=0;
int value=2;//此初始化值可以是除函数可以返回的数字以外的任何数字
while(值==2)
{
if(string1[i]>string2[i])
{
数值=1;
}
如果(string1[i]我在网上找到了这个
int strcmp(常量字符*s1,常量字符*s2)
{
对于(;*s1==*s2;s1++,s2++)
如果(*s1=='\0')
返回0;
返回((*(无符号字符*)s1<*(无符号字符*)s2)?-1:+1);
}
这一点,来自大师本身(,第2版,第106页):
//strcmp:如果st则返回>0
int strcmp(字符*s,字符*t)
{
int i;
对于(i=0;s[i]==t[i];i++)
如果(s[i]='\0')
返回0;
返回s[i]-t[i];
}
就是这样:
int strcmp(char *str1, char *str2){
while( (*str1 == *str2) && (*str1 != 0) ){
++*str1;
++*str2;
}
return (*str1-*str2);
}
如果您想要更快,可以在键入之前添加“register”,如下所示:
寄存器字符
然后,像这样:
int strcmp(register char *str1, register char *str2){
while( (*str1 == *str2) && (*str1 != 0) ){
++*str1;
++*str2;
}
return (*str1-*str2);
}
这样,如果可能,ALU的寄存器
// strcmp: return < 0 if s < t, 0 if s == t, > 0 if s > t
int strcmp(char *s, char *t)
{
int i;
for (i = 0; s[i] == t[i]; i++)
if (s[i] == '\0')
return 0;
return s[i] - t[i];
}
int strcmp(char *str1, char *str2){
while( (*str1 == *str2) && (*str1 != 0) ){
++*str1;
++*str2;
}
return (*str1-*str2);
}
int strcmp(register char *str1, register char *str2){
while( (*str1 == *str2) && (*str1 != 0) ){
++*str1;
++*str2;
}
return (*str1-*str2);
}