Sorting a string array

Okay fellas, I have been reading up on arrays and string array and I have a question I cannot seem to find the answer to.

I created a string string text[0] and it is defined by user input. I am trying to sort the input. I want Michael to read Macehil.

When I wasn't using an array and just a string I did this:

return sort(text.begin(), text.end()); and it worked fine. Do I need to change my string into a char? If so, would I static cast that?

Thanks guys.
I was able to get the length of the string by doing this:
1
2
3
4
long length(string text[0])
{
     return static_cast<char>(text[0].length());
}


but when I try this:
1
2
3
4
char sorted(string text[0])
{
     return static_cast<char>(sort(text[0].begin(), text[0].end()));
}

I get an error:
Static_cast from 'void' to 'int' is not allowed


Where is there a void? I am a little lost, although I do understand the length function is returning and int and the sorted function would be returning a char...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

int main()
{
    // sort a single string
    std::string text = "Michael" ;
    std::sort( text.begin(), text.end() ) ;
    std::cout << text << '\n' ;

    // sort an array of strings
    std::string array[5] { "zero", "one", "two", "three", "four" } ;
    std::sort( array, array+5 ) ;
    for( const auto& str : array ) std::cout << str << ' ' ;
    std::cout << '\n' ;

    // sort a vector of strings
    std::vector<std::string> vec { "five", "six", "seven", "eight", "nine" };
    std::sort( vec.begin(), vec.end() ) ;
    for( const auto& str : vec ) std::cout << str << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/d4a39f525eba7770
Thanks JLBorges, I know how to sort a single string but that you for the refresher. My problem is that my input is within a string array. Here is an example:

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
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string sortingFunction(string [0])

int main()
{
     string text[0];

     cout << "What is your name?"
     getline(cin, text[0]);
     cout << "You entered " << text[0] << endl;

     cout << "Your text sorted is: " << sorting function << endl;

return EXIT_SUCCESS;
}

[code]string sortingFunction(string [0])
{
     return sorting function
}


EDIT: My input into text[0] would be Michael and I would like the output to be Macehil.
Last edited on
This string text[0]; won't compile; an array can't have a size of zero.

This will:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
     string text ;

     cout << "What is your name? " ;
     getline( cin, text );
     cout << "You entered " << text << '\n' ;
     
     sort( text.begin(), text.end() ) ;
     cout << "Your text sorted is: " << text << endl;
}
Oh, so if I put string text[1] then the place of my text would reside in [0]right?

I was trying to make it an array because I also have to take the input and show the most occuring character, number of alpha characters, numeric characters, spaces, vowels and end marks.

It was to my knowledge that I needed my string in array form in order to do those things.

If I wanted to take a string and make it an array, how would one do that?
> I also have to take the input and show the most occuring character, number of alpha characters,
> numeric characters, spaces, vowels and end marks.
> It was to my knowledge that I needed my string in array form in order to do those things.

You do not need an array of strings. You need one string, which holds the sequence of characters entered by the user.
Ah, gotcha.. Thanks for that. I put everything into one string and I am trying to sort it but this is what I've come up with:
(my function)
1
2
3
4
string sorted(string text)
{
    return (sort(text.begin(), text.end()));
}


In my main function:
1
2
3
4
5
6
case 'D': cin.ignore();
                      cout << "Here is your text sorted: " << sorted(text) << endl;
                      cout << endl << "(Press Enter key to continue)" << endl;
                      cin.ignore(numeric_limits<streamsize>::max(), '\n');
                      myMenu();
                      break;


No viable conversion from 'void' to 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')


I don't understand where the void is..

Btw here are my variables and what not:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cctype>
#include <algorithm>
#include <string>
#include <ctime>
#include <locale>

using namespace std;

void myMenu();
long length(string text);
string text;
string sorted(string text);

int main()
{
   
    string text;
    long length(string text);
    string sorted(string text);
    char myChoice;
    myMenu();
> I don't understand where the void is..

std::sort() returns void.

If you must write a function,

1
2
3
4
5
6
string sorted(string text)
{
    // return (sort(text.begin(), text.end()));
    sort( text.begin(), text.end() ) ; // sort the text
    return text ; // return the sorted text
}


Thanks a ton for that... Before I tried making it a function, I just had it plugged directly where it was needed! But my prof. told me he wants all seperate functions and I did not know it returned a void.

Thanks again!
Topic archived. No new replies allowed.