Sorting an string in order

I tried to write a program to sort the elements of a string in alphabetical order. I don't know what got wrong::::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<string.h>
using namespace std;
int main()
{

    int c,x,y,z;
    char b;
    string m,n,o;
    cin>>m;
    x=m.length();

    for(y=0;y<=x;y++)
    {
        for(c=x;c==0;c--)//gets the place containing the highest
            {
                if(m[x]>=m[x-1])
                    {
                        b=m[x];
                        z=x;
                    }

            }
         for(c=0;c<=x;c++)//puts all the values except the highest one into n
         {
             if(c!=z)
             {
                 n[c]=m[c];
             }
             else
             {
                 c--;
             }

         }
         m=n;
         cout<<o[y];
    }
    cout<<"Here goes...";
    cout<<o;
    return 0;
}
There's an error in this place if(c!=z), because 'z' variable isn't initialized. In addition this:
1
2
3
4
5
if(m[x]>=m[x-1])
{
b=m[x];
z=x;
}
doesn't work properly. I'm not at home and i can't check it, but i think the program doesn't see m[x] as an integer. Check this out. Maybe it'll help.
Did the following..only shows another prompt after I enter the string,and nothing else happens..\\
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<string.h>
using namespace std;
int main()
{

    int c(0),x(0),y(0),z(0);
    char b;
    string m,n,o;
    cin>>m;
    x=m.length();

    for(y=0;y<=x;y++)
    {
        for(c=x;c==0;c--)//gets the place containing the highest
            {
                if(int(m[x])>=int(m[x-1]))
                    {
                        b=m[x];
                        z=x;
                    }

            }
         for(c=0;c<=x;c++)//puts all the values except the highest one into n
         {
             if(c!=z)
             {
                 n[c]=m[c];
             }
             else
             {
                 c--;
             }

         }
         m=n;

    }
    cout<<"Here goes...";
    cout<<o;
    return 0;
}

It would be a lot easier to follow your program if your variables had meaningful names consisting of more than a arbitrary letter.
I think that second for loop shouldn't start with 'x' value, but with 'x-1'. If you type "Name", m.length() gives you value 4. m[4] is NULL which ends char array.

Also, try cout<<int(m[x])<<endl; and tell what program prints on a screen.
Last edited on
Here is the best program which I have got on this website for sorting string alphabetically - http://www.aboutcomputer.org/showthread.php?t=392
1
2
3
#include <algorithm>

std::sort( str.begin(), std.end() );


Done in 1 line.
Better yet...
Edit: @Cody39e

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

static bool ch_lti(char c1,char c2) {return tolower(c1)<tolower(c2);}
int main()
{
  string str;
  getline(cin,str);
  sort(str.begin(),str.end(),ch_lti);
  cout << str << endl;
}
Last edited on
Topic archived. No new replies allowed.