Write a C++ program to sort characters (numbers and punctuation symbols are not included) in a string
- برمجة سي بلس بلس
- برمجة
- 2021-05-13
- MarwaMohammed
الأجوبة
#include <iostream>
#include <string>
using namespace std;
string sort_characters(string text) {
bool flag;
char ch;
do
{
flag = false;
for (int x = 0; x < text.length() - 1; x++)
{
if (text[x] > text[x + 1])
{
ch = text[x];
text[x] = text[x + 1];
text[x + 1] = ch;
flag = true;
}
}
} while (flag);
// Remove spaces
string str;
for (int y = 0; y < text.length(); y++)
{
if (text[y] != ' ')
{
str.push_back(text[y]);
}
}
return str;
}
int main() {
cout << "Original text: python \nSorted text: ";
cout << sort_characters("python") << endl;
cout << "\nOriginal text: AaBb \nSorted text: ";
cout << sort_characters("AaBb") << endl;
cout << "\nOriginal text: the best way we learn anything is by practice and exercise questions \nSorted text: ";
cout << sort_characters("the best way we learn anything is by practice and exercise questions") << endl;
return 0;
}أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال
