The length of the sequence is no more than 100 characters. It is considered that the sequence is read all, if 100 characters have already been read, or any other number of elements specified in advance by the user. Split the sequence into groups of 5 characters (in the last group, how many remain) and output each sequence in the reverse order.

Since we just started to study C, then there is almost no information on the Internet about how to do this by simple operators like the scan. I have presented an algorithm, but the program gives an incorrect answer, maybe you can figure out where the error is

#include #include #include #include #define N 100 int main() { int i, n,a,b; char C[N]; setlocale(0, ""); printf("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ количСство символов\n"); if (scanf("%d", &n) == 0) { printf("Π’Π²Π΅Π΄Π΅Π½ символ\n"); getch(); return 0; } if (n > N || n <= 0) { printf("НСдопустимоС число элСмСнтов\n"); getch(); return 0; } fflush(stdin); printf("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ массив элСмСнтов\n"); for (i = 0; i < n; i++) { scanf("%c", &C[i]); } printf("Π’Ρ‹ Π²Π²Π΅Π»ΠΈ: \n"); for (i = 0; i < n; i++) { printf("%c ", C[i]); } printf("\nΠ­Π»Π΅ΠΌΠ΅Π½Ρ‚Ρ‹ послС сортировки \n"); for (i = 0; i < n; i++) if (i+4 <= n) { a = i+4; for (a; a > i; a--) { printf("%c ", C[a]); } } else { a = i; for (n; n > a; n--) { printf("%c ", C[n]); } } getch(); return 0; } 

    1 answer 1

    Index i slowly add

     printf("\nΠ­Π»Π΅ΠΌΠ΅Π½Ρ‚Ρ‹ послС сортировки \n"); for (i = 0; i < n; i++) if (i+4 <= n) 

    it is necessary to add the index quickly

     printf("\nΠ­Π»Π΅ΠΌΠ΅Π½Ρ‚Ρ‹ послС сортировки \n"); for (i = 0; i < n; i+=5) if (i+4 < n) 

    Not all heels output:

     a = i+4; for (a; a > i; a--) 

    need all five:

     a = i+4; for (/*a*/; a >= i; a--) 

    Last batch of characters again with incorrect indexing:

     a = i; for (n; n > a; n--) 

    It is necessary to reduce by one and print the first one too:

     a = i; for (--n; n >= a; n--) 
    • You are super! But I have a couple of questions, if it’s not difficult for you, explain: printf ("\ nIts after sorting \ n"); for (i = 0; i <n; i + = 5) if (i + 4 <n) - why add the index so quickly? for (/ * a * /; a> = i; a--) - which means / * a * / - I'm completely oak, as you noticed, does not look like a pointer. a = i; for (--n; n> = a; n--) - and why should the index be reduced by one, even the first? - Limbos
    • The cycle on i indicates the first element of the five, inside the cycle one more cycle (it deals with printing of the five). And the main loop only provides the beginning of the fives: 0 , 5 , 10 , ... / * a * / - This is just the commented code - it does nothing and therefore deleted it. The elements of the array C [0], C [1], ..., C [n-1]. The number n of pieces. Therefore, we print from the end: C [n-1], ... - AlexGlebe