Advertisement
gasaichan

chars_shenanigans

Oct 20th, 2018
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <string.h>
  4. #include <conio.h>
  5.  
  6.  
  7.  
  8. // Функция для проверки, являются ли два слова одинаковыми
  9.  
  10. bool compare_string(char * a, char * b) {
  11.     if (strlen(a) != strlen(b)) {
  12.         return false;
  13.     }
  14.     else {
  15.         for (int i = 0; i < strlen(a); i++) {
  16.             if (a[i] != b[i]) {
  17.                 return false;
  18.             }
  19.         }
  20.         return true;
  21.     }
  22. }
  23.  
  24. // Функция для поиска элемента в массиве
  25.  
  26. bool find_in_array(int n, int * arr, int size) {
  27.     for (int i = 0; i < size; i++) {
  28.         if (arr[i] == n) {
  29.             return true;
  30.         }
  31.     }
  32.     return false;
  33. }
  34.  
  35. int main()
  36. {
  37.     setlocale(LC_ALL, "Russian");
  38.  
  39.     char str[512];
  40.     printf("Введите строку: ");
  41.     gets_s(str, 512);
  42.     printf("Введенная строка: ");
  43.     puts(str);
  44.  
  45.     int spaces = 0;
  46.     for (int i = 0; i < strlen(str); i++) {
  47.         if (str[i] == ' ') {
  48.             spaces++;
  49.         }
  50.     }
  51.  
  52.  
  53.     // Создание массива с количеством элементов = количество пробелов + 1. В строке всегда
  54.     // на 1 слово больше, чем пробелов
  55.  
  56.     char **words = new char*[spaces + 1];
  57.     for (int i = 0; i < spaces + 1; i++) {
  58.         words[i] = new char[32];
  59.     }
  60.  
  61.     // Запись каждого слова в массив words
  62.  
  63.     int count = 0;
  64.     int char_counter = 0;
  65.     for (int i = 0; i < strlen(str); i++) {
  66.         if (str[i] != ' ') {
  67.             words[count][char_counter] = str[i];
  68.             char_counter++;
  69.         }
  70.         else {
  71.             words[count][char_counter] = '\0';
  72.             char_counter = 0;
  73.             count++;
  74.         }
  75.     }
  76.  
  77.     // После окончания цикла последняя строка останется not null terminated. Исправляем
  78.  
  79.     words[spaces][char_counter] = '\0';
  80.  
  81.     // Составляем массив, содержащий индексы слов, которые уже встречались
  82.    
  83.     int * repeating_index = new int[spaces + 1];
  84.     int index = 0;
  85.     for (int i = 0; i < spaces; i++) {
  86.         for (int j = i + 1; j < spaces + 1; j++) {
  87.             if (compare_string(words[i], words[j])) {
  88.                 repeating_index[index] = j;
  89.                 index++;
  90.             }
  91.         }
  92.     }
  93.  
  94.    
  95.  
  96.     // Выводим те слова, которые не встречаются в массиве повторяющихся слов
  97.  
  98.     printf("Строка без повторяющихся слов: ");
  99.     for (int i = 0; i < spaces + 1; i++) {
  100.         if (!find_in_array(i, repeating_index, spaces + 1)) {
  101.             printf("%s ", words[i]);
  102.         }
  103.     }
  104.    
  105.  
  106.  
  107.  
  108.     _getch();
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement