Given a few lines. Arrange the lines in ascending order of their length and add to the end of each of them, separating with a “-”, their length.

github.com/Shmigel/Kep/blob/master/Program_18.c

#include <stdio.h> #include <string.h> #define N 10 int main(){ char s1[N], s2[N], s3[N], s[N * 3]; int i = 1, max, min; puts("Enter array1: "); fgets(s1,N,stdin); puts("Enter array2: "); fgets(s2,N,stdin); puts("Enter array3: "); fgets(s3,N,stdin); if( (strlen(s1) < strlen(s2)) && (strlen(s1) < strlen(s3)) ){ strcat(s,s1); }else if( (strlen(s2) < strlen(s1)) && (strlen(s2) < strlen(s3)) ){ strcat(s,s2); }else if( (strlen(s3) < strlen(s1)) && (strlen(s3) < strlen(s2)) ){ strcat(s,s3); } return 0; } 

All that I wrote, and then I do not know how to find the average length (I tried it but it did not work). I wanted to try to find the greatest and then the average of what remains, but I don’t know how to implement it correctly

Closed due to the fact that off-topic participants iksuy , rjhdby , Vlad from Moscow , user194374, HamSter 24 Dec '16 at 15:35 .

  • Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • five
    This question should be closed, because there is no magic word "and quickly!" ... About independent attempts to do something and there is no question ... - Harry
  • one
    And what is the problem? Sorting or modifying strings? - VladD
  • @Harry, already there. - Qwertiy
  • @Qwertiy Withdraw your vote. - Harry

1 answer 1

Let's now make an array of pointers to your lines.

 char * arr[3] = { s1, s2, s3 }; 

Now we need to call to sort qsort into which to transfer
1. arr array
2. number of elements in it 3
3. size of the element sizeof(char*)
4. and the comparison function to which the addresses of the elements are transmitted.

 int cmp(const void * p1, const void * p2) { char * s1 = *(char**)p1; // p1 - указатель на указатель на char char * s2 = *(char**)p2; // p2 - указатель на указатель на char int len1 = strlen(s1); int len2 = strlen(s2); // Сравниваем длины if (len1 > len2) return 1; if (len1 < len2) return -1; return 0; } 

It turns out something like

 #include <stdio.h> #include <string.h> #include <stdlib.h> #define N 10 int cmp(const void * p1, const void * p2) { char * s1 = *(char**)p1; char * s2 = *(char**)p2; int len1 = strlen(s1); int len2 = strlen(s2); if (len1 > len2) return 1; if (len1 < len2) return -1; return 0; } int main(){ char s1[N] = "First", s2[N] = "Second", s3[N] = "One"; char * arr[3] = { s1, s2, s3 }; qsort(arr,3,sizeof(char*),cmp); for(int i = 0; i < 3; ++i) printf("%s\n",arr[i]); return 0; } 

Well, to add length to each line - provided that the buffer allows you to place additional characters there! - you can use, for example, sprintf ( snprintf1 или sprintf_s` is safer, of course, but you yourself ...):

 void addLen(char* s) { int len = strlen(s); // Пишем, начиная с len-того символа (добавляем к строке) sprintf(s+len," - %d",len); } 

Those. end of main can be written, for example, as

 for(int i = 0; i < 3; ++i) { addLen(arr[i]); printf("%s\n",arr[i]); } 

just not forgetting to increase the N - 10 characters is not enough ...

Then you can do anything with an ordered array of pointers to strings - collect in one line, display, rewrite - in a word, of your choice :)

If I, of course, truly understood the task.

And yet - you can even immediately create an array of strings of type char s[N][M]; and sort it, rather than an array of pointers — by changing the parameters passed to qsort and the comparator, respectively.

  • return len1 - len2; :-) - PinkTux
  • @PinkTux So - more understandable to a beginner. - Harry