It is required to make a program for comparing (for more-less) two entered lines. The comparison must be performed in a separate function. Moreover, the function should not contain input-output structures. How to make the output of the result in occur in the function main , and not compare :

void compare(char *str, char *str1) { int i, k; //Считаем количество символов for(i = 0; str[i] != '\0'; i++); for(k = 0; str1[k] != '\0'; k++); //Сравниваем и выводим результат if(i == k) printf("Одинаковые\n"); else { if(i > k) printf("1-ая больше\n"); else printf("2-ая больше\n"); } } int main() { setlocale (LC_CTYPE,"rus"); // подключение русского языка char str[82], str1[82]; //Вводим строки printf("Введите str не больше 80 символов: "); gets(str); printf("Введите str1 не больше 80 символов: "); gets(str1); //Обращаемся к функции compare(str, str1); return 0; } 
  • Well, I will do the compare function so that I return for example int: 0 if they are equal, 1 if more than 1, 2 if more than 2 lines. Then through the switch check in the main that the function returned ... - greshnik

3 answers 3

As always, I post the original solution:

 const char* compare(const char* str1, const char* str2) { const char* const strResults[3]={"Вторая длиннее", "Одинаковые по длине", "Первая длиннее"}; int result=strlen(str1)-strlen(str2); return strResults[(result>=0)+(result>0)]; } int main() { ... printf(compare(str1, str2)); } 

Another variant of the implementation of compare came up with (based on another answer):

 int compare(const unsigned char* s1, const unsigned char* s2) { while(*s1 && *s2) s1++, s2++; return !!*s1+!*s2; } 
  • This original! Replace the string comparison function with the string length comparison function ... Moreover, in the original version ... - alexlz
  • > Replace the string comparison function with the string length comparison function ... So he should have judged it by his post. - devoln
  • Alas. The text did not peer. - alexlz

Well, it's more typical to return values ​​(-1, 0, 1).

 int compare(char *str1, char *str2) { while(*str1 && *str2 && *str1 == *str2) str1++, str2++; if(*str1 == *str2) return 0; return (*str1 < *str2) ? -1 : 1; } 

    Just shorter (as asked in the question we compare exactly the length of the lines)

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

    Returns 0 if the string lengths are equal, <0 - the first one is shorter,> 0 the second one is shorter.

    • You have to cast the lines to unsigned char *. It is better to just make return strlen(str1)-strlen(str2) and get the difference in the lengths of the strings. And this function will return incomprehensibly what - the code of the last character of the smaller string with a plus or a minus. - devoln
    • one
      If with a plus - the first is longer. With a minus - the second. - alexlz
    • The compiler (gcc) itself normally causes char * to unsigned char *. At least I didn’t have any warnings. @GLmonster, the last line of the answer says what the result is when it returns. And two strlen () is an extra job. Imagine a situation where one line is empty and the other is many megabytes. - avp
    • > the last line of the answer says what the result is when it returns. If with a plus - the first is longer. With a minus - the second. I know that. I mean that the return value, except for being longer or shorter, does not carry any useful information. A variant with the subtraction of the lengths of the lines shows how exactly one line is longer than the other. - devoln
    • So the task was not set. In general, it is in such a formulation, somehow pulled over the ears. - avp