I enter a sentence and at the output I want to get the reverse word order. There was an idea to make two arrays. The original array with the proposal to consider from the end and write to the new array (where the reverse word order should already be). But with the implementation did not work. Then he decided to do it differently.

#include<stdio.h> #include<conio.h> #include<string.h> void main(void) { int i = 0, j = 0, k = 0, l = 0; char A[60]; gets_s(A); puts(A); l = strlen(A); while (l>=0) { j = j + 1; if (A[l] == 32) { while (k <= j) { printf("%c", A[l + ++k]); } k = 0; j = 0; } l--; } _getch(); } 

Just display words from the end, focusing on the space

  • jfs

2 answers 2

That's not going to suit you?

 #include <stdlib.h> #include <stdio.h> #include <string.h> #include <ctype.h> int iswhite(int c) { return !c || isspace(c) || ispunct(c); } void swap(char* a, char * b) { char t = *a; *a = *b; *b = t; } void reverseWord(char * b, char * e) { while(b < e) { swap(b++,e--); } } char * reverse(char * s) { reverseWord(s,s+strlen(s)-1); for(char* c = s; *c; ++c) { if (iswhite(*c)) continue; char * b = c; while(!iswhite(*c)) ++c; reverseWord(b,c-1); } return s; } char s[] = "Привет, это длинное предложение!"; int main(int argc, const char * argv[]) { puts(reverse(s)); } 

    Here, it worked

     #include<stdio.h> #include<conio.h> #include<string.h> void main(void) { int i = 0, j = 0, k = 0, l = 0, t = 0; char A[60]; gets_s(A); puts(A); l = strlen(A); while (l>=0) { j = j + 1; if (A[l] == 32 ) { while (k <= j) { printf("%c", A[l + k++]); } k = 0; j = 0; } l--; } while (A[t] != 32) t++; for (j = 0; j < t;j++) { printf("%c", A[j]); } _getch(); } 

    Thought it all the same