Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged


Introduction

In this tutorial, we introduce an approach to sorting an alphanumeric string such that the position of alphabets and numbers remains unchanged. In this approach, we use C++ functions and take an input string containing characters and numbers. Generate the string without changing the alphabet and number positions. The newly sorted string contains the shuffled characters in alphabetical order, and arranging the numbers will keep their positions the same.

Example 1

String = “”tutorials43points”
Output = aiilnoopr34sstttu

In the above example, the input string is rearranged without changing the position of the number 32. The characters are sorted alphabetically, and the numbers are arranged in increasing order.

Example 2

String = “India16love”
Output = adeii16lnov

In the above example, the input string starts with the character India and ends with the character “love”, the number 16 is between 2 characters. Sort both characters in alphabetical order without changing the position of the number 16.

We use C++ programming concepts and some library functions as listed

  • isdigit()

  • isalpha()

  • end()

  • begin()

  • sort()

Syntax

isdigit(int value);

isdigit() is a library function defined in the cctype header file for the characters. It checks whether the input string contains numbers or not. If the string contains no number, it returns none else this function returns 0.

isalpha(int value);

isalpha() is defined in the cctype header file used for the characters. It is used to check whether the passed parameter contains an alphabet or not. It returns 0 if there is no alphabet in the parameter, otherwise, it returns a non-zero value.

end() function is used to pass the pointer to the last element of the input string. It is defined in the C++ standard library.

begin() function passes the pointer to the starting element of the parameter.

sort() function in C++ is an inbuilt library function to sort or arrange the input elements into increasing order.

Algorithm

  • Take an input string

  • Use the isdigit() function to find numbers in the input string and store them in a separate string.

  • Use the alpha () function to find alphabets in the input alphanumeric string and store them in a separate string.

  • Sort the two strings separately using the sort() function.

  • Merge the two sorted strings while iterating over them.

  • Print the resulting string.

Example

We take an input string “tutorials43point” which contains characters and numbers. The approach sorts the string without changing the positions of characters and numbers.

#include <iostream>
#include <algorithm>
#include <cctype>

int main(){
   std::string s = "tutorials43point";
   std::string nums, chars;

   // Separate the alphanumeric string into numbers and alphabets
   for (char ch : s) {
      if (isdigit(ch)){
         nums += ch;
      }
      else if (isalpha(ch)){
         chars += ch;
      }
   }
   // Sort the alphabets and numbers separately
   std::sort(chars.begin(), chars.end());
   std::sort(nums.begin(), nums.end());

   // Merge the sorted alphabets and numbers back into the original string
   int i = 0, j = 0;
   for (char& ch : s){
      if (isdigit(ch)){
         ch = nums[j++];
      } 
      else if (isalpha(ch)){
         ch = chars[i++];
      }
   }
   std::cout << "The new string is : "<< s << std::endl;
   return 0;
}

Output

The new string is : aiilnoopr34stttu

Conclusion

In this article, we implemented an example using C++ library functions to arrange a given string without changing the alphabet and number positions. The input string characters are arranged in alphabetical order and the number is displayed in increasing order. Different library functions like isdigit(), isalpha(), end(), begin(), and sort() are used in the approach.

Updated on: 01-Aug-2023

526 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements