0

How to arrange every characters in a string in alphabetical order with C++ or Java?

for example: Apple the output should be Aelpp

13th Dec 2017, 4:01 AM
John Ryan Samonte
John Ryan Samonte - avatar
3 odpowiedzi
+ 8
String input = "apple"; char[] charArray = input.toCharArray(); Arrays.sort(charArray); String sortedString = new String(charArray); System.out.println(sortedString); or input="apple"; char[] charArray = input.toCharArray(); length = charArray.length(); for(int i=0;i<length;i++){ for(int j=i+1;j<length;j++){ if (charArray[j] < charArray[i]) { char temp = charArray[i]; charArray[i]=arr[j]; charArray[j]=temp; } } }
13th Dec 2017, 4:10 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 4
In java: char [] in = inputString.toCharArray(); Arrays.sort(in); string out = new String(in); System.out.println(out); where inputString your string.
13th Dec 2017, 4:11 AM
Dima Makieiev
Dima Makieiev - avatar
+ 2
#include <iostream> #include <string> #include <algorithm> int main() { std::string str = "fedcba"; std::sort(str.begin(), str.end()); std::cout << str; }
13th Dec 2017, 5:24 AM
aklex
aklex - avatar