Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <string.h>
- using namespace std;
- void delete_spaces(char *string, char *packed_string)
- {
- unsigned int i, k;
- for (i = 0, k = 0; i < strlen(string); i++)
- {
- if (string[i] != ' ')
- {
- packed_string[k] = string[i];
- k++;
- }
- }
- }
- bool IsPolindrome(char *string)
- {
- bool flag = true;
- unsigned int i;
- for (i = 0; i < strlen(string) / 2; i++)
- if (string[i] != string[strlen(string) - i - 1])
- flag = false;
- return flag;
- }
- void WIDE(char *string, char *replace_string, int size)
- {
- unsigned int i, j, k;
- int space_counter = 0;
- int need_space;
- int counter = 0;
- int string_length = strlen(string);
- // Считаем пробелы
- for (i = 0; i < string_length; i++)
- {
- if (string[i] == ' ')
- space_counter++;
- }
- if (space_counter == 0) space_counter = 1;
- // На каждый пробел - ставим M пробелов
- need_space = (size - string_length) / space_counter + 1;
- for (i = 0, k = 0; i < string_length; i++)
- {
- if (string[i] != ' ')
- {
- replace_string[k] = string[i];
- k++;
- }
- else
- {
- for (j = 0; j < need_space; j++)
- {
- if (strlen(replace_string) < size)
- {
- replace_string[k]=' ';
- k++;
- }
- }
- }
- }
- replace_string[k] = '\0';
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- int k;
- bool IsPolindrom;
- char string[80] = { 0 }, without_spaces[80] = { 0 }, sec_string[80] = { 0 }, replace_string[80] = { 0 };
- cout << "Фраза: ";
- cin.getline(string, 80);
- delete_spaces(string, without_spaces); // удаляем все разделители
- IsPolindrom = IsPolindrome(without_spaces);
- if (IsPolindrom == true)
- cout << "Палиндром" << endl;
- else
- cout << "Не палиндром" << endl;
- cout << "Строка: ";
- cin.getline(sec_string, 80);
- cout << "Количество пунктов выравнивания: ";
- cin >> k;
- WIDE(sec_string, replace_string, k);
- cout << replace_string << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement