how to make a program in c++[using Array] that ask the user to input 9 letters and arrange it in alphabetical order. thanks!
#include <iostream> #include <string> using namespace std; int main() { string str = ""; cout << "input : "; cin >> str; { for(int i = 0; i < str.size(); i++) { for(int j = i + 1; j < str.size(); j++) { if(str[i] > str[j]) { char temp = str[i]; str[i] = str[j]; str[j] = temp; } } } cout << "output: " << str << "\n"; } }
Here's an implementation that's more specific (it uses an array of characters, not a string object): http://ideone.com/LqqJgD
when i tried your program the output is equal to the imputed char. the program should arranged the imputed char in to alphabetical order.??
Whose program?
The code you posted. when i started debugging it works fine. but when i started imputing letters the output didn't satisfies the problem. it did not arrange the imputed letters into alphabetical order. it only prints the first char imputed.i don't know what's wrong with it.
The code is working correctly with the Ideone compiler. Perhaps your input is incorrect?
Join our real-time social learning platform and learn together with your friends!