beginner - func & string help

This is a discussion on beginner - func & string help within the C++ Programming forums, part of the General Programming Boards category; this supposed to return any string in alpha order my thought is to read the entire string and compare two ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    1

    beginner - func & string help

    this supposed to return any string in alpha order
    my thought is to read the entire string and compare two chars a time, then sort in order
    I realize I'm not returning anything yet, but I'm stuck on the working func...any thoughts?

    #include <iostream>
    #include <string>
    using namespace std;

    void alpha (string &s);

    int main()
    {
    string text;
    cout<<"Please enter a string: ";
    cin >> text;

    alpha (text);

    cout <<"Alphabetized: " ;
    return 0;
    }

    void alpha (string &s)
    {
    string text;
    int str_length = text.size();

    getline(cin, text);
    for (int i = 0; i<str_length; i++)
    {
    for (int j=i+1;j<str_length ;j++ )
    {
    if (s[i] != ' ' || s[j] != ' ')
    {
    for (int k = j; k < str_length; k++)
    {
    if (s[k] < s[j])
    cout << s[j];
    else
    cout << s[k];
    }
    }
    }
    }
    }

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    All you have to do is just sort s like a reguar array in
    alpha. You don't have to return anything because you've passed
    s in by referance. Also you shouldn't have to check for whitespace because cin operator<< will normally stop reading at the first whitespace.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    Reading in the input as a string is fine. Remember you can use array notation on a string variable,

    string text = "hello";
    cout<<text[0]<<endl;

    So, all you need to do is sort the array index values. char types are stored as integer codes, and you can look them up in an ASCII Code Table to get an idea of what's going on. Therefore, all you are trying to do is sort a bunch of numbers in order. There are algorithms for that, e.g. bubble sort, etc., or you can figure it out yourself, although optimizing the sort so it takes the fewest number of steps probably isn't something within most people's ability.

    The basic idea is you compare index position 0 and 1 and switch the largest value to position 1. Then you compare index position 1 and 2 and switch the largest value to position 2. Continuing in that fashion you will push the largest(or smallest) value to the end of the array. Then you go through the whole process again, and you will end up pushing the next largest value to the 2nd to the last index position in the array.

    One optimization: the second time through the array, you don't need to check the last position in the array because you know it's the largest value in the entire array already. Similarly, the third time through the array, you don't need to check the last two positions in the array because you already know they contain the second largest and largest values in the array, and so on.

    Are you supposed to be able to enter multiple words? When you read input into a string variable using cin, cin will only read one word.
    Last edited by 7stud; 04-06-2003 at 09:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21