Advertisement
daniil_mironoff

Ex. 6.5 (1)

May 22nd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. // ЗАДАНИЕ 6.5 (1)
  2. // Дана строка. Найти буквы, встречающиеся в тексте больше двух раз
  3.  
  4. #include <iostream>          // Для ВВОДА и ВЫВОДА
  5.  
  6. using namespace std;         // ПРОСТРАНСТВО ИМЁН
  7.  
  8. int main() {
  9.     char * str = new char[1];
  10.     cout << "Enter word: ";
  11.     scanf("%s", str);
  12.  
  13.     // Объявления массивов для букв
  14.     char * arr_chars        = new char[26];
  15.     int  * arr_counts_chars = new int [26];
  16.    
  17.     // Счетчик (уникальных) букв
  18.     int count_chars = 0;
  19.  
  20.     // Перебор символов вводимой строки
  21.     for (int i = 0; str[i] != 0; i++) {
  22.        
  23.         // Проверка на букву
  24.         if (str[i] < 65 || str[i] > 122 || (str[i] > 90 && str[i] < 97)) {
  25.             continue;
  26.         }
  27.        
  28.         // Приравнивает заглавный буквы к прописным
  29.         if (str[i] < 97) {
  30.             str[i] += 32;
  31.         }
  32.        
  33.         // Флаг на наличие буквы в массиве
  34.         bool flag = false;
  35.        
  36.         // Перебор ранее найденых букв
  37.         for(int j = 0; j < count_chars; j++) {
  38.             // Если буква была найдена раньше
  39.             if (arr_chars[j] == str[i]) {
  40.                 flag = true;
  41.                 arr_counts_chars[j] += 1;
  42.                 break;
  43.             }
  44.         }
  45.        
  46.         if (!flag) {
  47.             arr_chars[count_chars] = str[i];
  48.             arr_counts_chars[count_chars] = 1;
  49.             count_chars++;
  50.         }
  51.     }
  52.    
  53.    
  54.     cout << "Repeat chars:";
  55.    
  56.     for(int i= 0; i < count_chars; i++) {
  57.         // Если буква была найдена раньше
  58.        
  59.         if (arr_counts_chars[i] > 2) {
  60.             cout << " " << arr_chars[i];
  61.         }
  62.     }
  63.    
  64.     cout << endl;
  65.    
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement