Replace every consonant sequence with its length in the given string


This article will help us understand how to replace the consecutive consonant sequence with its length in the given string. Consonants are series of alphabetical letters that are not vowels. Here we need to first identify which letter in the string are consonants.

For example, in the word “abcdiopqrsu”, the consonant sequence “bcd” and “pqrs”. Next, we would replace each consonant sequence with its length. So the word “bcd” would replace with “3” because there are three consecutive consonants, and the same with the word “pqrs” replace with “4” as there are four consecutive consonants.

Algorithm

  • First, we will define a function ‘isConsonant()’ which accepts a character value as a parameter to verify it for consonant and returns the result in the form of a Boolean value. If the given character is a consonant this function returns TRUE and otherwise false.

    Explanation of logic used to find the consonant character

    (con == 'a' || con == 'e' || con == 'i' || con == 'o' || con == 'u'):

    • The con is the name of the variable.

    • ==: The equal to operator sets the vowel value to the variable.

    • ||: Using the bitwise logical OR it will allow more than one vowel to set the value in the variable ‘con’.

  • We will start with definining the variable ‘string’ in the main function, and storing value ‘abcdiopqrsu’ in the string variable. Then we will take an empty string variable ‘result’. The function iterates through each character in the string using a for loop and for each character it checks if it is a consonant by calling the ‘isConsonant’ function.

  • If the character is consonant then it will enter the while loop and continue the iteration as long as it finds the next consonant. During each iteration of the while loop, the counter variable ‘counter’ will be incremented. Once finish the while loop the function will add the value of the counter to the result string using the ‘to_string’ function.

  • Then we check if the character is not consonant then the function simply adds the character to the ‘result’ string.

  • Finally, we will be printing the value of the result string using the cout statement

Example

In this program, we are going to understand how to replace the consonant and provide the length instead of it.

#include<iostream>
#include<string>
using namespace std;
bool isConsonant(char con) {
   //Check whether the given character is consonant or not.
   return !( con == 'a' || con == 'e' || con == 'i' || con == 'o' || con == 'u');
}
int main() {
   string str = " abcdiopqrsu";
   string result;
   for( int i=0; i < str.length(); i++) {
      if ( isConsonant(str[i]) ) {
         //Here we have to find the consonant and count its length.
         int counter = 1;
         while( isConsonant( str[i+1] ) ) {
            counter++;
            i++;
         }
         result += to_string( counter );
      } else {
         result += str[i];
      }
   }
    cout<< result << endl ;
    return 0;
}

Output

1a3io4u

Conclusion

We explored the concept of consonant sequence with its length in the given string. We saw how to check the consonant character by using == “equal to” and || “bitwise logical OR”. Then we set the string variable and calculate the non-consonant character by its total count. The following application used like text processing, data compression, and pattern recognition.

Updated on: 20-Apr-2023

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements