Sorting of characters is too easy with this function in C++.
#include <iostream> using namespace std; int main () { string str = "Shirley"; // It sorts the characters and stores in the same string. // Sorting happens based on the ASCII value. sort(str.begin(), str.end()); cout << str << endl; return 1; }
Takeaways :
- It sorts the characters and stores it in the same string.
- Sorting happens based on the ASCII values.
I also wanted to show you that we can copy a string to character.
#include <iostream> #include <sstream> #include <cstring> using namespace std; int main() { stringstream line("I often post some programming contents"); string word = ""; char arr[1] = {'\0'}; int count = 0; while (getline(line, word, ' ')) { count++; cout << word << endl; if (count == 1) { strcpy(arr, word.c_str()); cout << " First word is " << arr << endl; } } return 0; }
Output
I First word is I often post some programming contents
Comments
Post a Comment