Home  >  Article  >  Backend Development  >  Replace each consonant sequence in the given string with its length

Replace each consonant sequence in the given string with its length

PHPz
PHPzforward
2023-09-08 22:05:02760browse

Replace each consonant sequence in the given string with its length

This article will help us understand how to replace a sequence of consecutive consonants in a given string with its length. A consonant is a series of letters that are not vowels. Here, we first need to determine which letters in the string are consonants.

For example, in the word "abcdiopqrsu", the consonant sequences "bcd" and "pqrs". Next, we will replace each consonant sequence with their length. So the word "bcd" would be replaced by "3" because there are three consecutive consonants, similarly the word "pqrs" would be replaced by "4" because there are four Continuous consonants.

algorithm

  • First, we will define a function 'isConsonant()', which accepts a character value as a parameter to verify whether it is a consonant, and returns the result as a Boolean value. This function returns TRUE if the given character is a consonant, false otherwise.

    Looking for logical explanations of consonant characters

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

    • con is the name of the variable.

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

    • ||: Using the bitwise logical OR operator allows multiple vowels to set the value of the variable 'con'.

    We will start by defining the variable 'string' in the main function and storing the value 'abcdiopqrsu' in the string variable. Then, we will use an empty string variable 'result'. The function iterates over 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 a consonant, enter the while loop and continue iterating when the next consonant is found. During each iteration of the while loop, the counter variable 'counter' will be incremented. After completing the while loop, the function will add the value of the counter to the resulting string using the 'to_string' function.

  • We then check if the character is not a consonant and the function simply adds that character to the "result" string.

  • Finally, we will use the cout statement to print the value of the resulting string

The translation of

Example

is:

Example

In this program we will learn how to replace consonants and provide their length.

#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

in conclusion

We explored the concept of consonant sequences and their length in a given string. We saw how to use "equals" (==) and "bitwise logical OR" (||) to check for consonant characters. Then we set the string variable and count the non-consonant characters by its total number. The following applications are used for text processing, data compression, and pattern recognition.

The above is the detailed content of Replace each consonant sequence in the given string with its length. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete