
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Alternate Lower Upper String Sort in C++
A string is an array of characters. And this problem is to sort the elements of the string with alternate upper and lower case.
Problem Description − Alternate lower upper string sort, is a problem in which we are provided an unordered string which contains mixed upper and lowercase characters and we need to sort this string in such a way that upper and lower case characters are placed in alternate positions but are in a sorted manner.
Let’s take an example to understand the topic better,
Input : aFegrAfStRzsV Output : AaFeRfSgVrstz Explanation : Upper case characters : A F R S V Lower case characters : a e f g r s t z
Both are in a sorted manner so we can now place them in alternate order.
Now, since we have understood the problem. Let's create a solution for it. One way is creating arrays for both uppercase and lowercase letters in sorted order and then alternating them in the final string. We have created an algorithm based on this logic.
Algorithm
Step 1 : In an array lowercount[] add all lowercase characters in sorted order. Step 2 : In an array uppercount[] add all uppercase characters in sorted order. Step 3 : Create the final string using alternate values from both arrays. Step 4 : Print the result array.
Example
#include <iostream> using namespace std; #define MAX 26 void alternateULSort(string& s) ; int main(){ string str = "aFegrAfStRzsV"; cout<<"The unsorted string is : "<<str; cout<<"\nThe alternate lower upper sorted string is "; alternateULSort(str); cout << str << "\n"; } void alternateULSort(string& s){ int n = s.length(); int lowerCount[MAX] = { 0 }, upperCount[MAX] = { 0 }; for (int i = 0; i < n; i++) { if (isupper(s[i])) upperCount[s[i] - 'A']++; else lowerCount[s[i] - 'a']++; } int i = 0, j = 0, k = 0; while (k < n) { while (i < MAX && upperCount[i] == 0) i++; if (i < MAX) { s[k++] = 'A' + i; upperCount[i]--; } while (j < MAX && lowerCount[j] == 0) j++; if (j < MAX) { s[k++] = 'a' + j; lowerCount[j]--; } } }
Output
The unsorted string is : aFegrAfStRzsV The alternate lower upper sorted string is AaFeRfSgVrstz
- Related Articles
- Convert vowels from upper to lower or lower to upper using C program
- Program for converting Alternate characters of a string to Upper Case.\n
- Java program to count upper and lower case characters in a given string
- C# program to count upper and lower case characters in a given string
- Swap Upper diagonal with Lower in C++
- isupper(), islower(), lower(), upper() in Python and their applications
- Sum of upper triangle and lower triangle in C++
- C program to convert upper case to lower and vice versa by using string concepts
- Upper or lower elements count in an array in JavaScript
- MySQL Query to change lower case to upper case?
- Upper bound and Lower bound for non increasing vector in C++
- Plotting only the upper/lower triangle of a heatmap in Matplotlib
- Alternate casing a string in JavaScript
- How to convert Lower case to Upper Case using C#?
- How to convert Upper case to Lower Case using C#?
