A string consisting of any words is entered, for example:
"My name is qwe."
How to swap words to get it:
"qwe is my name."
All I could do was swap letters. They advised me to find words, that is, to check for the presence of characters before the space, but how should I transfer the whole word to another line, in the correct order and to the right place? This is only possible by character. But in this case, it is the same thing that I did. Already quite desperate. Any ideas?
PS First course, so the problem should be solved on the basis of the simplest functions.
Addition:
You, apparently, do not understand what level of the program I need, everything is super-super simple. Thanks to everyone who answered, but it’s even easier, without creating your own functions, without complex loops, without atypical functions. The only suitable option written is to turn each word from an already completely inverted line. Can someone tell me exactly how to implement IT? Code attached:
#include<stdio.h> #include<math.h> #include<string.h> #define N 50 void main() { char str1[N]; char str2[N]; int i1 = 0, i2, l; printf("Vvedite stroku: "); gets(str1); strcpy(str2,str1); l = strlen(str1); i2 = l - 1; for(i1, i2; i1 <= l - 1; i1++, i2--) { str1[i1] = str2[i2]; } printf("Ishodnaya stroka: %s\n",str2); printf("Preobrazovannaya: %s\n",str1); system("pause"); } It turns out like this:
ewq si eman yM
Done, you can close, thanks for all the help (no). True, it is necessary before the first and after the last words to put spaces in order to work.
#include<stdio.h> #include<math.h> #include<string.h> #define N 50 void main() { char str1[N]; char str2[N]; char str3[N]; int i1=0,i2,l,k1=0,k2=0,k3; printf("Vvedite stroku: "); gets(str1); strcpy(str2,str1); l = strlen(str1); i2=l-1; for (i1,i2;i1<=l-1;i1++,i2--) { str2[i1]=str1[i2]; } i1=0;i2=l-1; strcpy(str3,str2); for (i1;i1<=N;i1++) { k2=i1; k3=i1; if (str2[i1]==' ') { for (k1,k2;k1<=k3;k2--,k1++) { str3[k1]=str2[k2]; } k1=k3; k2=k3; } } printf("Ishodnaya stroka: %s\n",str1); printf("Preobrazovannaya: %s\n",str2); printf("Preobrazovannaya: %s\n",str3); system("pause"); }