Skip to main content
Post Reopened by Phrancis, Billal BEGUERADJ, 200_success, Snowhawk, Daniel
edited tags; edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Sort string's characters into alphebeticalphabetic order

added namespace that OP forgot to include
Source Link
Phrancis
  • 20.5k
  • 6
  • 70
  • 155

Any improvements that I can make with this code? Also, is there another way to convert a vector to a string variable without using a for ranged-based loop? From the posts I've seen for converting, I only saw printing the vector instead of returning a string.

#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

std::string AlphabetSoup(string str) {
std::vector<char>letters;

 istringstream iss(str);

 copy(istream_iterator<char>(iss),
        istream_iterator<char>(),
        back_inserter(letters));

 std::sort(letters.begin(),letters.end());

 //Regrouping the characters into a string
 std::string orderedString;
 for(auto element : letters)
 {
     orderedString += element;
 }

 return orderedString;
}

Any improvements that I can make with this code? Also, is there another way to convert a vector to a string variable without using a for ranged-based loop? From the posts I've seen for converting, I only saw printing the vector instead of returning a string.

#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>

std::string AlphabetSoup(string str) {
std::vector<char>letters;

 istringstream iss(str);

 copy(istream_iterator<char>(iss),
        istream_iterator<char>(),
        back_inserter(letters));

 std::sort(letters.begin(),letters.end());

 //Regrouping the characters into a string
 std::string orderedString;
 for(auto element : letters)
 {
     orderedString += element;
 }

 return orderedString;
}

Any improvements that I can make with this code? Also, is there another way to convert a vector to a string variable without using a for ranged-based loop? From the posts I've seen for converting, I only saw printing the vector instead of returning a string.

#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

std::string AlphabetSoup(string str) {
std::vector<char>letters;

 istringstream iss(str);

 copy(istream_iterator<char>(iss),
        istream_iterator<char>(),
        back_inserter(letters));

 std::sort(letters.begin(),letters.end());

 //Regrouping the characters into a string
 std::string orderedString;
 for(auto element : letters)
 {
     orderedString += element;
 }

 return orderedString;
}
Post Closed as "Not suitable for this site" by Snowhawk, Billal BEGUERADJ, Graipher, Ludisposed, Incomputable
Simplify title
Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

Take a string being passed and return the string in alphabetically Sort string's characters into alphebetic order

Added #include <>
Source Link
austingae
  • 1.1k
  • 1
  • 9
  • 22
Loading
Source Link
austingae
  • 1.1k
  • 1
  • 9
  • 22
Loading