Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Произвести выравнивание по правому краю введенного текста, для чего к каждой строке применить функцию WIDE(str,k), которая равномерно вставляет пробелы между словами так, чтобы длина строки str стала равной k. (Величина k должна быть больше длины самой длинной строки текста.)*/
- #pragma warning(disable:4018)
- #include <iostream>
- #include <Windows.h>
- #include <string.h>
- using namespace std;
- void WIDE(char *string, char *replace_string, int size)
- {
- unsigned int i, j, k;
- char space[1] = { ' ' };
- 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 flag;
- char string[80] = { 0 }, packed_string[80] = { 0 }, sec_string[80] = { 0 }, replace_string[80] = { 0 };
- 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