0

Can anyone plz explain this code in easy words?

Here is code, about sorting the string array // C++ program to sort a string of characters #include<bits/stdc++.h> using namespace std; const int MAX_CHAR = 26; // function to print string in sorted order void sortString(string &str) { // Hash array to keep count of characters. // Initially count of all characters is // initialized to zero. int charCount[MAX_CHAR]={0}; // Traverse string and increment // count of characters for (int i=0; i<str.length(); i++) // 'a'-'a' will be 0, 'b'-'a' will be 1, // so for location of character in count // array we will do str[i]-'a'. charCount[str[i]-'a']++; // Traverse the hash array and print // characters for (int i=0;i<MAX_CHAR;i++) for (int j=0;j<charCount[i];j++) cout << (char)('a'+i); } // Driver program to test above function int main() { string s = "cdab"; sortString(s); return 0; }

10th May 2022, 5:34 PM
Muhammad Talha Atif
Muhammad Talha Atif - avatar
1 Answer
+ 3
Did you notice that there already are comments that explain every step? Add cout to see the values.
10th May 2022, 5:40 PM
Lisa
Lisa - avatar