Advertisement
gasaichan

chars_flip

Oct 20th, 2018
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <string.h>
  4. #include <conio.h>
  5.  
  6. int main()
  7. {
  8.     setlocale(LC_ALL, "Russian");
  9.  
  10.     char str[512];
  11.     printf("Введите строку: ");
  12.     gets_s(str, 512);
  13.     printf("Введенная строка: ");
  14.     puts(str);
  15.  
  16.     int spaces = 0;
  17.     for (int i = 0; i < strlen(str); i++) {
  18.         if (str[i] == ' ') {
  19.             spaces++;
  20.         }
  21.     }
  22.  
  23.  
  24.     // Создание массива с количеством элементов = количество пробелов + 1. В строке всегда
  25.     // на 1 слово больше, чем пробелов
  26.  
  27.     char **words = new char*[spaces + 1];
  28.     for (int i = 0; i < spaces + 1; i++) {
  29.         words[i] = new char[32];
  30.     }
  31.  
  32.     // Запись каждого слова в массив words
  33.  
  34.     int count = 0;
  35.     int char_counter = 0;
  36.     for (int i = 0; i < strlen(str); i++) {
  37.         if (str[i] != ' ') {
  38.             words[count][char_counter] = str[i];
  39.             char_counter++;
  40.         }
  41.         else {
  42.             words[count][char_counter] = '\0';
  43.             char_counter = 0;
  44.             count++;
  45.         }
  46.     }
  47.  
  48.     // После окончания цикла последняя строка останется not null terminated. Исправляем
  49.  
  50.     words[spaces][char_counter] = '\0';
  51.  
  52.  
  53.     // Выводим все слова из массива в обратном порядке
  54.  
  55.     printf("Слова перевертыши: \n");
  56.     for (int i = 0; i < spaces + 1; i++) {
  57.         for (int j = strlen(words[i]); j >= 0; j--) {
  58.             printf("%c", words[i][j]);
  59.         }
  60.         printf("\n");
  61.     }
  62.  
  63.  
  64.     _getch();
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement