top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to sort the alphanumeric string?

+1 vote
250 views

Say I have given strings like
a100
b20
abc123

and I want to sort them what should be the approach.

posted Jul 15, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

There's nothing odd about the sort of alphanumeric string; '1' will always come before '2' and '2' will always come before '3' and so on.

Since in your example you dont have too many numbers but say you have a100 and a200 so a100 will always be less then a200 it is the way strcmp is defined.

So in short write the normal sorting program (whatever the way you like i.e. quicksort, bubblesort etc) and use strcmp for the comparion and keep this in mind for strcmp -

Syntax
int strcmp(const char *str1, const char *str2)

Return Value
if Return value is < 0 then it indicates str1 is less than str2
if Return value is > 0 then it indicates str2 is less than str1
if Return value is = 0 then it indicates str1 is equal to str2

answer Jul 18, 2014 by Salil Agrawal
Similar Questions
+3 votes

Say the given string is ABC

Output should be ABC ACB BAC BCA CBA CAB

+6 votes

For example: It returns ‘b’ when the input is “abaccdeff”.

+8 votes

Convert the given string into palindrome with minimum number of appends(at end of the given string). O(n) algorithm will be appreciated ??

Input :=> Malayal
Output :=> Malayalam

...