Compiling a Char array without using strcmp in C

I need a way to compare 2 char arrays without strcmp. Is that the way? Or am I missing something? When I compiled it, if I type the same lines in both, the program gets stuck and does nothing. PLEASE, HELP!

EDIT: CHANGE WHAT IT MEAN TO BE A i, not C

int compare_info(char *array1, char *array2) { int i; i = 0; while(array1[i] == array2[i]) { if(array1[i] == '\0' || array2[i] == '\0') break; i++; } if(array1[i] == '\0' && array2[i] == '\0') return 0; else return-1; } 
+5
source share
6 answers

Here you have a solution, it is prety like your code, but I made some changes. I pulled the returns in the middle of the cycle because they destroy the structure, so it is easier to analyze. Finishing, I added a new condition to while, so when the end of the line is found, the loop ends

 int compare_info(char *array1, char *array2) { int i; int response = 0; i = 0; while(array1[i] == array2[i] && response == 0 ) { if(array1[i] == '\0' || array2[i] == '\0'){ response = 1; } i++; } return response; } 
+3
source

Here you have a solution

 int compare_info(char *array1, char *array2) { if ((array1 == NULL) || (array2 == NULL)) return 0; while ((*array1 != '\0') && (*array2 != '\0') && (*array1 == *array2)) { array1++; array2++; } return (*array1 == *array2); } 

or maybe you will like it more

 int compare_info(char *array1, char *array2) { int i; i = 0; if ((array1 == NULL) || (array2 == NULL)) return 0; while ((array1[i] != '\0') && (array2[i] != '\0') && (array1[i] == array2[i])) { i++; } return (array1[i] == array2[i]); } 

you can make const arguments, this will be the best style, because if you want to compare strings only, you guarantee that the function will not change them, I mean compare_info(const char *array1, const char *array2)

+3
source

In addition to the other answers, I will add the following to the mix as strcmp appearance without strcmp :

 int strcmp_nohdr (char *s1, char *s2) { if (!s1 && !s2) return 0; if (s1 && !s2) return 1; if (!s1 && s2) return -1; while (*s1 && *s2 && *s1 == *s2) s1++, s2++; if (!*s1 && !*s2) return 0; else if (*s1 > *s2) return 1; else return -1; } 

examples / output:

 $ strcmp_nohdr mydog mycat s1 is greater than s2 $ strcmp_nohdr mybat mycat s1 is less than s2 $ strcmp_nohdr mycat mycat s1 is equal to s2 $ strcmp_nohdr mycat myca s1 is greater than s2 $ strcmp_nohdr myca mycat s1 is less than s2 

Another alternative could be a call and assembly procedure that does the same thing.

+2
source

Unlike some people, I prefer to catch null pointers as early as possible (through a program crash and debugger), so I avoid any checks for null pointers below. In other words, do not pass a null pointer to these functions.

The str_neq function determines if the strings are not equal, which seems to be the correct logic based on your code (return 0 / false if equal and -1 / true if not equal):

 int str_neq (const char *s1, const char *s2) { while (*s1 != '\0' && *s1 == *s2) ++s1, ++s2; return -(*s1 != *s2); } 

To ensure the same behavior as strcmp , a small change is needed in the expression that evaluates the return value:

 int str_compare (const char *s1, const char *s2) { while (*s1 != '\0' && *s1 == *s2) ++s1, ++s2; return *s1 - *s2; } 

Hope this helps! :-)

+1
source

Short:

 int compare_info(char * array1, char * array2) { char * ptr1 = array1; char * ptr2 = array2; while(*ptr1 && *ptr2 && *ptr1++==*ptr2++); if(!*ptr1 && !*ptr2 && *(ptr1-1)==*(ptr2-1)) return 0; return -1; } 

When two arrays are the same with a return of 0, else returns -1. (It is not equal to strcmp.)

0
source

The following function should mimic the exact behavior of strcmp :

 int compare_info(const char* array1,const char* array2) { int i; for (i=0; array1[i]!=0 && array2[i]!=0; i++) { if (array1[i] > array2[i]) return +1; if (array1[i] < array2[i]) return -1; } if (array1[i] != 0) return +1; if (array2[i] != 0) return -1; return 0; } 
0
source

Source: https://habr.com/ru/post/1209180/


All Articles