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"); } 
  • If words have the same length - there are no problems, if different - you need to shift the characters from one word to another. - nick_n_a
  • one
    To bother less, it's easier to create a string with new characters based on the old one. - nick_n_a
  • This is all clear, I need an algorithm. I have been thinking for 2 days and digging up the entire Internet, until I found a single intelligible answer. - vosure
  • @StupidStudent. And you do not dig the Internet, but a textbook on C. How the line is in memory. - Sublihim
  • And what should I give it? I know what a string is and how it is stored in memory, what's next? I need to move the WORD, and not the character. How can I do this? - vosure

5 answers 5

First, flip the entire line by letter, then search for individual words and turn them back over.

  • Letters are turned, how does the search for individual words occur? The program does not know the length of the words and the correct arrangement of the letters in the string. But I say, the whole point is that the task is of a general form, you cannot simply turn them over manually. The algorithm is needed. - vosure
  • 2
    A cool option, by the way, you find the beginning and end of a word and work between them with the same algorithm that unfolded the entire line. - Oleg Brezhnev
  • @OlegBrezhnev, yeah, I also thought about it when I read the answer. - Qwertiy
  • How to find it? Words can be 100 pieces, then I need to enter 200 variables to memorize the ordinal number of the beginning and end of each word? - vosure
  • one
    so for this and came up with arrays! - KoVadim

As I understand it, you need to write a program using normal cycles. Below is a demonstration program showing how this can be done. Word delimiters in the program are the space character (' ') and tab character ( '\t' )

The program first counts the number of characters located to the point that completes the sentence, since the point must remain in its original place.

Then the whole sentence, excluding the full stop, is reversed.

Then, in the cycle, the beginnings and ends of words are searched for, which in turn are reversed. The reverse of the whole sentence and individual words is done.

 #include <stdio.h> size_t find( const char *s, char c ) { size_t i = 0; while ( s[i] && s[i] != c ) i++; return i; } char * reverse( char *s, size_t n ) { for ( size_t i = 0; i < n / 2; i++ ) { char c = s[i]; s[i] = s[ni-1]; s[ni-1] = c; } return s; } int main(void) { char s[] = "My name is qwe."; size_t n = find( s, '.' ); puts( s ); reverse( s, n ); for ( size_t i = 0; i != n; ) { while ( s[i] == ' ' || s[i] == '\t' ) i++; if ( i != n ) { size_t j = i; while ( ++i != n && s[i] != ' ' && s[i] != '\t' ); reverse( s + j, i - j ); } } puts( s ); return 0; } 

Output of the program to the console

 My name is qwe. qwe is name My. 

    Here is a sketch of the recursion code. It will work, there are no obvious leaks (there may be realloc, but this usually goes beyond the limits of the school course). Checked in gcc. The very first line is needed exclusively for it, so that the compiler does not swear. By the way, the original string is modified, but this is an algorithm cost. You can do without it :)

     #define _GNU_SOURCE 1 #include <stdio.h> #include <string.h> #include <stdlib.h> char * rev(char * str) { char * pos = strchr(str, ' '); if (pos == NULL) { return strdup(str); } else { char * r = rev(pos+1); *pos = '\0'; r = realloc(r, strlen(r) + 1 + strlen(str) + 1); r = strcat(r, " "); return strcat(r, str); } } int main() { char * x = strdup("My name is qwe"); char * y = rev(x); if (y) { printf("%s\n", y); } free(x); free(y); return 0; } 
    • Thanks, of course, but here the situation is like with C # code - this is not going to work, the program should work on the simplest functions. - vosure
    • so the functions are very simple. easier nowhere. Or announce the list of "simple" functions. - KoVadim
    • No, I really can't understand. This code is already a different level of programming, it can be seen with the naked eye, not the level of one semester at all. Such things will not be accepted. Using functions in the function realloc (), free (). What is all this? It is me even in the compiler does not start. - vosure
    • realloc, malloc, free is memory allocation-release. These are very basic things for a C programmer. - KoVadim
    • For a programmer of course, but not for the level of the 1st semester. It is simply not necessary there. - vosure

    You can, for example, select each word in a separate line, i.e., make an array of strings. Then output them in the correct order. You can copy words using strncpy. Search for words from the space or the beginning of the line to the space or the end of the line. That is, there will be two indices - which indicates the beginning of the word, and which indicates the symbol following the word.

    • Each line is declared, we do not know how many users will enter words 3,5,125. It is possible to fit the solution to a specific condition, but it is necessary that the program is solved under any condition. - vosure
    • Dynamically allocate memory, malloc and free functions - Oleg Brezhnev

    http://ideone.com/Yz5KwH

     #include <stdio.h> #include <string.h> int group(char ch) { return isalpha(ch) ? 0 : isspace(ch) ? 1 : 2; } void word(char **r, char *s, int g) { for (; *s && group(*s) == g; ++s) *((*r)++) = *s; } int main(void) { char str[256], res[256], *s, *r; int next; while (gets(str)) { r = res; for (next=group(s=str+strlen(str)-1); ; --s) if (s == str) { word(&r, s, group(*s)); break; } else if (group(*s) != next) { word(&r, s+1, next); next = group(*s); } *r = 0; puts(res); } return 0; }