Advertisement
Balda

Untitled

Jan 9th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. void delete_spaces(char *string, char *packed_string)
  7. {
  8.     unsigned int i, k;
  9.     for (i = 0, k = 0; i < strlen(string); i++)
  10.     {
  11.         if (string[i] != ' ')
  12.         {
  13.             packed_string[k] = string[i];
  14.             k++;
  15.         }
  16.     }
  17. }
  18.  
  19. bool IsPolindrome(char *string)
  20. {
  21.     bool flag = true;
  22.     unsigned int i;
  23.     for (i = 0; i < strlen(string) / 2; i++)
  24.         if (string[i] != string[strlen(string) - i - 1])
  25.             flag = false;
  26.         return flag;
  27. }
  28.  
  29.  
  30. void WIDE(char *string, char *replace_string, int size)
  31. {
  32.     unsigned int i, j, k;
  33.     int space_counter = 0;
  34.     int need_space;
  35.     int counter = 0;
  36.     int string_length = strlen(string);
  37.     // Считаем пробелы
  38.     for (i = 0; i < string_length; i++)
  39.     {
  40.         if (string[i] == ' ')
  41.             space_counter++;
  42.     }
  43.     if (space_counter == 0) space_counter = 1;
  44.     // На каждый пробел - ставим M пробелов
  45.     need_space = (size - string_length) / space_counter + 1;
  46.     for (i = 0, k = 0; i < string_length; i++)
  47.     {
  48.         if (string[i] != ' ')
  49.         {
  50.             replace_string[k] = string[i];
  51.             k++;
  52.         }
  53.         else
  54.         {
  55.         for (j = 0; j < need_space; j++)
  56.             {
  57.                 if (strlen(replace_string) < size)
  58.                 {
  59.                     replace_string[k]=' ';
  60.                     k++;
  61.                 }
  62.             }
  63.         }
  64.     }
  65.     replace_string[k] = '\0';
  66. }
  67.  
  68. int main()
  69. {
  70.    
  71.     SetConsoleCP(1251);
  72.     SetConsoleOutputCP(1251);
  73.     int k;
  74.     bool IsPolindrom;
  75.     char string[80] = { 0 }, without_spaces[80] = { 0 }, sec_string[80] = { 0 }, replace_string[80] = { 0 };
  76.     cout << "Фраза: ";
  77.         cin.getline(string, 80);
  78.     delete_spaces(string, without_spaces); // удаляем все разделители
  79.     IsPolindrom = IsPolindrome(without_spaces);
  80.     if (IsPolindrom == true)
  81.         cout << "Палиндром" << endl;
  82.     else
  83.         cout << "Не палиндром" << endl;
  84.     cout << "Строка: ";
  85.         cin.getline(sec_string, 80);
  86.     cout << "Количество пунктов выравнивания: ";
  87.         cin >> k;
  88.     WIDE(sec_string, replace_string, k);
  89.     cout << replace_string << endl;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement