Tính độ dài và sắp xếp xâu ký tự

1092
Lập trình C++

Bài toán.  Cho một xâu ký tự s. Yêu cầu: Ghi lại, tính độ dài xâu và sắp xếp lại các ký tự trong xâu s. Ví dụ:

STRING.INP

STRING.OUT

ABEHGCDVIM

ABEHGCDVIM

10

ABCDEGHIMV

Code tham khảo:

#include <iostream>
#include <cstring>
using namespace std;
int main() {
    freopen("STRING.INP","r",stdin);
    freopen("STRING.OUT","w",stdout);
    string s;
    char temp;
    cin >> s;
    cout << s << endl;
    int m = s.length();
    cout << s.length() << endl;
    for(int i=1; i<=m-2; i++)
        for(int j=i+1;j<=m-1;j++)
            if(s[i] > s[j]) {
                temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
    for(int i=0;i<=m-1;i++) cout<<s[i];
    return 0;
}