can we do sorting in chars in c++
like
if acdb is input
and abcd as output
any one provide me prog for this ...........
or Sample code:
plz...........

Dani AI

Generated

Yes — characters can be sorted exactly like numbers in C++. If the characters are in a std::string, char[], or std::vector<char>, the simplest idiom is std::sort on the character range:

#include <algorithm>
#include <string>
#include <iostream>

int main() {
    std::string s = "acdb";
    std::sort(s.begin(), s.end());
    std::cout << s << '\n'; // prints: abcd
}

As observed, sorting compares the characters' numeric code values. 's question about storage matters only for syntax — std::sort works on any random-access container or pointer range. Prefer std::sort over hand-rolled bubble sort (noted by ) for clarity and speed; see std::sort docs for details.

For alphabetic or case-insensitive ordering use a comparator. Important: cast char to unsigned char before passing to std::tolower to avoid undefined behavior:

#include <algorithm>
#include <cctype>
#include <string>

std::sort(s.begin(), s.end(), [](char a, char b){
    return std::tolower(static_cast<unsigned char>(a)) <
           std::tolower(static_cast<unsigned char>(b));
});

If the alphabet size is small (e.g., only ASCII letters) and the string is very large, counting sort is linear and simple:

int counts[256] = {};
for (char ch : s) ++counts[static_cast<unsigned char>(ch)];
std::string out; out.reserve(s.size());
for (int i = 0; i < 256; ++i) out.append(counts[i], static_cast<char>(i));

Caveats: sorting raw bytes is fine for ASCII, but not for multi-byte UTF-8 or linguistic collation — use ICU or proper locale/collation APIs for Unicode-sensitive ordering. For details on std::tolower behavior see std::tolower docs. 's pointer to general sorting resources is also useful if different sorting algorithms are of interest.

Recommended Answers

All 6 Replies

How are the char stored---individual free standing variables, string (C style vs STL string object vs other string object implementation like CString or APSTring), or individual char in an array/list/vector/stack, whatever?

Characters can be easily sorted the way numbers are, since they are numbers.

can any one provide me a sample code for it ........?????
plz

can any one provide me a sample code for it ........?????
plz

Sample code for what? A sorting algorithm? Sorting characters as opposed to integers (as Mosaic mentions, characters are treated as integers, only they don't have as big of a range). Lots of sample code out there. Check the code snippets section, google "C++ bubble sort", stuff like that, but be more specific if you've tried these things and haven't found the results. A bubble sort implementation is pretty short.

If you know how to sort numbers, then just change the ranges to the appropriate characters ASCII values(or some other standard).

http://www.asciitable.com/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.