@adaskl

Как вывести только русские символы?

Найти и вывести через пробел в алфавитном порядке русские буквы. После введения с клавиатуры произвольного текста отсортировать его и вывести на экран через пробел в алфавитном порядке русские символы. Например ----> Введите строку: SJR гвба 123. Пока что программа выводит все символы в алфавитном порядке, а надо только русские.
Результат: а б в г
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <windows.h>
#include <locale>

char  tolower_rus(char c)
{
    return  tolower(c,std::locale(""));
}

std::string  string_tolower_rus(const std::string& str)
{
    std::string   res_str = str;

    std::transform(str.begin(),str.end(),res_str.begin(), tolower_rus);

    return  res_str;
}


std::string foo(std::string str)
{
    std::string newStr(str);
    char temp1, temp2;
    for (auto it = newStr.begin(); it != newStr.end(); it++)
    {
        if (*it == ' ')
        {
            newStr.erase(it);
        }
    }


    for (auto it = str.begin(); it != str.end(); it++)
    {
        if (*it == ' ')
        {
            str.erase(it);
        }
    }

    newStr = string_tolower_rus(newStr);
    for (int i = 0; i < str.size(); i++)
    {
        for (int j = 0; j < str.size() - 1; j++)
        {
            if (newStr[j] > newStr[j + 1])
            {
                temp1 = str[j];
                temp2 = newStr[j];
                str[j] = str[j + 1];
                newStr[j] = newStr[j + 1];
                str[j + 1] = temp1;
                newStr[j + 1] = temp2;
            }
        }
    }
    return str;
}
int main()
{
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    std::string str;
    std::getline(std::cin, str);
    str = foo(str);
    for (auto x : str)
    {
        std::cout << x << " ";
    }
    return 0;
}
  • Вопрос задан
  • 192 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы