0
How to arrange every characters in a string in alphabetical order with C++ or Java?
for example: Apple the output should be Aelpp
4 Respostas
+ 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; } } }
+ 4
In java:
char [] in = inputString.toCharArray();
Arrays.sort(in);
string out = new String(in);
System.out.println(out);
where inputString your string.
+ 2
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string str = "fedcba";
std::sort(str.begin(), str.end());
std::cout << str;
}