The task itself looks completely like this: "after one empty line, you are prompted to enter line A (input is done by pressing the Enter key), and then line B. After that, output the processed lines. If the lines consist of the same characters, remove the Latin characters from B and Russian letters; otherwise, arrange the characters A in the reverse order of the alphabetic. " I have problems only with the ordering of the characters A in the reverse order of the alphabetic c ++ PS The connection of other libraries is not considered

#include <iostream> #include <windows.h> #include <string> using namespace std; int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); setlocale(LC_ALL, "Russian"); string a,b; cout << "Введите строку А : "; getline(cin,a); cout << "Введите строку B : "; getline(cin,b); int j = a.size(); char * writable = new char[a.size() + 1]; cout << "А : " << a << endl; cout << "B : " << b << endl; const char*str1 = a.c_str(); const char*str2 = b.c_str(); int count1[256] = { 0 }; int count2[256] = { 0 }; for (const char*pos = str1;*pos;++pos) ++count1[(unsigned char)*pos]; for (const char*pos = str2;*pos;++pos) ++count2[(unsigned char)*pos]; bool res = true; for (int i = 0;i < 256;++i) res = res && count1[i] == count2[i]; if (res == true) { for (int unsigned i = 0; i < b.size();i++) { char ch = b[i]; if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || (((ch >= 'а') && (ch <= 'я')) || ((ch >= 'А') && (ch <= 'Я')))) { b.erase(i, 1); --i; } } cout << "B : " << b << endl; res = false; } else { /*Тут как раз не могу реализовать сортировку символов строки А в обратном алфавитному порядке*/ for (int i = 0; i < sizeof(writable) - 2;i++) { for (int j = 0; j < sizeof(writable) - 2 ;j++) { if (writable[j] < writable[j + 1]) { tmp = writable[j]; writable[j] = writable[j + 1]; writable[j + 1] = tmp; } } } cout << "A : " << writable; } system("pause"); return 0; } 
  • Take the standard sort - or is it not provided by the rules of the game? - Harry
  • You cannot use other libraries, and sort is in another library - Logvas Channel
  • Then write sorting - sorting type by inserts, bubble or some other sort - for example, there are several ready-made options. - Harry
  • I tried to sort the bubble, but I get some kind of porridge like "NNNNNNNeeeee ..." ... What's wrong? Shouldn't he compare character codes? - Logvas Channel

1 answer 1

Your entire code:

 std::string a,b; getline(std::cin,a); getline(std::cin,b); // если в строке `a` не нашли символ, который нет в строке `b` if (a.find_first_not_of(b) == std::string::npos) { for (size_t i = 0; i < b.length(); ++i) //проверяем является ли символ буквой if(isalpha(b[i])) //функцию можете писать сами b.erase(i--, 1); std::cout << a << std::endl << b; } else { //сортируем в обратном порядке sort(a.rbegin(), a.rend()); // функцию сортировки можете написать сами std::cout << a; } 

PS how to sort the sequence, there are numerous ways, examples of which you will find in the internet and in SO . And yet: when working with std::string , do not try to go to the C_string if this is not necessary. And then you immediately go to work with an array, ignoring the advantages and capabilities of std::string

An example of sorting a character string (if spaces are not taken into account). Each symbol is placed in the sequence by its int_type index, then, starting from the end, we assign all non-spaces to the elements of the source line:

 std::string t(255, ' '); const size_t sz = a.length(); for(size_t i = 0; i < sz; ++i) t[+a[i]] = a[i]; // +a[i] то же, что int(a[i]) //теперь в t элементы распологаются в возрастающем порядке //не считая пробелы вытаскиваем с конца for(size_t i = 254, j = 0; j < sz; --i) { if(t[i] != ' ') a[j++] = t[i]; } 

which you can write instead of sort(a.rbegin(), a.rend());