Strcmp implementation

I tried to implement strcmp :

 int strCmp(char string1[], char string2[] ) { int i=0,flag=0; while(flag==0) { if (string1[i]>string2[i]) { flag=1; } else if (string1[i]<string2[i]) { flag=-1; } else { i++; } } return flag; } 

but I am stuck in the fact that the user enters the same lines, because the function works with 1 and -1, but does not return 0. Can anyone help? And please, no pointers!

+7
c strcmp
source share
7 answers

It seems you want to avoid pointer arithmetic, which is a pity, as this makes the solution shorter, but your problem is that you are scanning out of line. An explicit gap will be added. Your program is slightly modified:

 int strCmp(char string1[], char string2[] ) { int i = 0; int flag = 0; while (flag == 0) { if (string1[i] > string2[i]) { flag = 1; } else if (string1[i] < string2[i]) { flag = -1; } if (string1[i] == '\0') { break; } i++; } return flag; } 

Shorter version:

 int strCmp(char string1[], char string2[] ) { for (int i = 0; ; i++) { if (string1[i] != string2[i]) { return string1[i] < string2[i] ? -1 : 1; } if (string1[i] == '\0') { return 0; } } } 
+2
source share

Umm .. too complicated. Go for it:

 int strCmp(const char* s1, const char* s2) { while(*s1 && (*s1 == *s2)) { s1++; s2++; } return *(const unsigned char*)s1 - *(const unsigned char*)s2; } 

It returns <0, 0, or> 0 as expected

You cannot do this without pointers. In C, indexing an array is done using pointers.

Perhaps you want to avoid using the * operator *

+18
source share

First of all, the standard C function strcmp compares string elements as being of type unsigned char .

Secondly, parameters must be pointers to constant strings in order to provide comparisons also for constant strings.

A function can be written as follows:

 int strCmp( const char *s1, const char *s2 ) { const unsigned char *p1 = ( const unsigned char * )s1; const unsigned char *p2 = ( const unsigned char * )s2; while ( *p1 && *p1 == *p2 ) ++p1, ++p2; return ( *p1 > *p2 ) - ( *p2 > *p1 ); } 
+4
source share

These are 10 opcode implementations of strcmp (presumably GCC)

 int strcmp_refactored(const char *s1, const char *s2) { while (1) { int res = ((*s1 == 0) || (*s1 != *s2)); if (__builtin_expect((res),0)) { break; } ++s1; ++s2; } return (*s1 - *s2); } 

You can try this implementation and compare it with others https://godbolt.org/g/ZbMmYM

+1
source share

Your problem is that you do not detect the end of the line and therefore do not return zero if both lines end before any difference is detected.

You can simply fix this by checking this in the loop condition:

 while( flag==0 && (string1[i] != 0 | string2[i] != 0 ) ) 

Note that both lines are checked, because if only one is at the end, the lines are not equal, and comparisons within the loop should detect this.

Please note that comparing characters may not give the result you expect. For one, he didn’t determine whether the char signed or unsigned, so you should probably use unsigned char for comparison.

Perhaps a cleaner solution would be to immediately return when you find the difference, and instead of flag = -1 you will return -1 directly. But it is rather a matter of opinion.

0
source share

Taken from here .

 #include<stdio.h> #include<string.h> //using arrays , need to move the string using index int strcmp_arry(char *src1, char *src2) { int i=0; while((src1[i]!='\0') || (src2[i]!='\0')) { if(src1[i] > src2[i]) return 1; if(src1[i] < src2[i]) return 1; i++; } return 0; } //using pointers, need to move the position of the pointer int strcmp_ptr(char *src1, char *src2) { int i=0; while((*src1!='\0') || (*src2!='\0')) { if(*src1 > *src2) return 1; if(*src1 < *src2) return 1; src1++; src2++; } return 0; } int main(void) { char amessage[] = "string"; char bmessage[] = "string1"; printf(" value is %d\n",strcmp_arry(amessage,bmessage)); printf(" value is %d\n",strcmp_ptr(amessage,bmessage)); } 

I made a few changes to make it work as strcmp .

0
source share
 int strCmp(char string1[], char string2[] ) { int i=0,flag=0; while(flag==0) { if (string1[i]>string2[i]) { flag=1; } else if (string1[i]<string2[i]) { flag=-1; } else if (string1[i]==string2[i]) { return 0; } else { i++; } } return flag; } 
-one
source share

All Articles