Home >Backend Development >C++ >Decode the given string by removing recurring characters
The purpose of this article is to implement a program to decode a given string by removing recurring characters.
As you know what a string is, a string is nothing but a collection of characters. Additionally, there is no limit to the number of times characters can be repeated in a string. The same character can appear multiple times in a string. In this article, we will find a way to decode a given encoded string str by removing duplicate occurrences.
The goal is to decode the provided string str that has been used with one occurrence of 'a', two occurrences of 'b', three occurrences of 'c', four occurrences of 'd', up to occurrence 26 of 'z' coded once.
A program to decode a given string by removing duplicate occurrences.
Note − Do not ignore spaces that may be included in the letter.
Let us take the input string str = “abbbb accc”
The output obtained is: abb acThe translation of
Each letter is written according to the number of times it appears in the English alphabet. The resulting string is "abb acc" because the letter b is repeated four times. The letter a is repeated twice, and finally the letter c is repeated three times.
Also in this case, spaces are not ignored.
Let us take the input string str = “ddddadddd”
The output obtained is: dadThe translation of
Each letter is written according to the number of times it appears in the English alphabet. The resulting string is "dad" because the letter d is repeated eight times and the last letter a appears only once.
In this case, there are no spaces between characters.
Let us take the input string str = “abbccc”
The output obtained is: abcThe translation of
Each letter is written taking into account the number of times it appears in the English alphabet. The resulting string is "abc" because the letter a appears only once. The letter b is repeated twice and finally the letter c is repeated three times.
In this case, there are no spaces between characters.
In order to decode a given string by removing repeated characters, we adopt the following method in this article.
The method to solve this problem and decode a given string by removing duplicate occurrences is based on iterating the string.
That is, the above problem can be solved by iterating the string str and pushing each character into the output string, then moving forward by that position to find the next character.
The algorithm for printing the number of camelCase characters present in a given string is given below
In order to resolve this issue, please follow the instructions listed below -
First Step − Start
Step 2 - Define the string
Step 3 - Create a variable named result with an initial value of an empty string to store the output string.
Step 4 - Create function findOccurences(char a1) and perform subsequent operations -
Step 5 - If the value of a1 falls between a and z, return the value of a1 as "a". If the value range of a1 is not in the range A to Z, then the value of a1 is returned as "Z". If not, 0 is returned.
Step 6 - Define the function decodeTheString(string s) to decode the string s
Step 7 - After completing the above stages, print the string result as the final string.
Step 8 − Stop
This is a C program that implements the algorithm written above to decode a given string by removing recurring characters
// C++ program for our above algorithm #include <bits/stdc++.h> using namespace std; // Function to count the number of occurences of each character int findOccurences(char a1){ // If the character is a lower case , that is [a-z] if (a1 <= 'z' && a1 >= 'a') { return a1 - 'a'; } // If the character is an uppercase, that is [A-Z] else if (a1 <= 'Z' && a1 >= 'A') { return a1 - 'A'; } // If the character is something else like a punctuation mark then return 0; } // Function used for decoding the given string str void decodeTheString(string s){ string result = ""; // Iterate through the provided string str for (int i = 0; i < s.length(); i++) { result.push_back(s[i]); // Find the index i of the next characterto be printed i += findOccurences(s[i]); } cout << "The decoded string: " << result << endl; } int main(){ string s = "aaabbbb"; cout << "Input string: "<< s << endl; decodeTheString(s); return 0; }
Input string: aaabbbb The decoded string: aaabb
Similarly, we can decode any given string by removing duplicate occurrences of it.
This article addresses the challenge of decoding any given string by removing duplicate occurrences of that string. Provided here is the C programming code along with the algorithm to decode any given string by removing duplicate occurrences of that string.
The above is the detailed content of Decode the given string by removing recurring characters. For more information, please follow other related articles on the PHP Chinese website!