Home > Backend Development > C++ > body text

Decode the given string by removing recurring characters

PHPz
Release: 2023-08-25 21:29:06
forward
1173 people have browsed it

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.

Problem Statement

A program to decode a given string by removing duplicate occurrences.

Note − Do not ignore spaces that may be included in the letter.

Example Example 1

Let us take the input string str = “abbbb accc”
Copy after login
The output obtained is: abb ac
Copy after login
The translation of

Explanation

is:

Explanation

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.

Example 2

Let us take the input string str = “ddddadddd”
Copy after login
The output obtained is: dad
Copy after login
The translation of

Explanation

is:

Explanation

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.

Example 3

Let us take the input string str = “abbccc”
Copy after login
The output obtained is: abc
Copy after login
The translation of

Explanation

is:

Explanation

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.

method

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.

algorithm

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

Example: C program

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;
}
Copy after login

Output

Input string: aaabbbb
The decoded string: aaabb
Copy after login

in conclusion

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!