Table of Contents
algorithm
Example
Output
in conclusion
Home Backend Development C++ Replace each consonant sequence in the given string with its length

Replace each consonant sequence in the given string with its length

Sep 08, 2023 pm 10:05 PM
replace consonant The extracted programming keywords are: string

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

Output

1a3io4u
Copy after login

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Master PyCharm replacement shortcut keys in 5 minutes and easily increase your programming speed! Master PyCharm replacement shortcut keys in 5 minutes and easily increase your programming speed! Feb 22, 2024 am 10:57 AM

PyCharm is a commonly used Python integrated development environment with rich functions and shortcut keys that can help developers improve programming efficiency. In the daily programming process, mastering PyCharm's shortcut key replacement skills can help developers complete tasks more quickly. This article will introduce you to some commonly used replacement shortcut keys in PyCharm to help you easily improve your programming speed. 1.Ctrl+R replacement In PyCharm, you can use the Ctrl+R shortcut key to perform replacement operations.

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

Use java's StringBuilder.replace() function to replace a specified range of characters Use java's StringBuilder.replace() function to replace a specified range of characters Jul 24, 2023 pm 06:12 PM

Use java's StringBuilder.replace() function to replace a specified range of characters. In Java, the StringBuilder class provides the replace() method, which can be used to replace a specified range of characters in a string. The syntax of this method is as follows: publicStringBuilderreplace(intstart,intend,Stringstr) The above method is used to replace the index star from

PyCharm Beginner's Guide: Comprehensive Analysis of Replacement Functions PyCharm Beginner's Guide: Comprehensive Analysis of Replacement Functions Feb 25, 2024 am 11:15 AM

PyCharm is a powerful Python integrated development environment with rich functions and tools that can greatly improve development efficiency. Among them, the replacement function is one of the functions frequently used in the development process, which can help developers quickly modify the code and improve the code quality. This article will introduce PyCharm's replacement function in detail, combined with specific code examples, to help novices better master and use this function. Introduction to the replacement function PyCharm's replacement function can help developers quickly replace specified text in the code

PyCharm replaces shortcut keys to make programming more convenient! PyCharm replaces shortcut keys to make programming more convenient! Feb 21, 2024 pm 12:03 PM

PyCharm is an integrated development environment popular among programmers. It provides powerful functions and tools to make programming more efficient and convenient. In PyCharm, reasonable setting and replacement of shortcut keys is one of the keys to improving programming efficiency. This article will introduce how to replace shortcut keys in PyCharm to make programming more convenient. 1. Why should we replace shortcut keys? In PyCharm, shortcut keys can help programmers quickly complete various operations and improve programming efficiency. However, everyone has different habits, and some people may

How to replace a word in Excel using Python? How to replace a word in Excel using Python? Sep 16, 2023 pm 10:21 PM

In Python, we can replace one word with another word in Excel using a third-party Python library called openpyxl. Microsoft Excel is a useful tool for managing and analyzing data. Using Python, we can automate some Excel data management tasks. In this article, we will learn how to replace a word in Excel using Python. Before installing openpyxl to replace Word in Excel, we need to install the openpyxl library in the system using the Python package manager. To install openpyxl, enter the following command in the terminal or command prompt. Pipinst

How to use the REPLACE function to replace a specified part of a string in MySQL How to use the REPLACE function to replace a specified part of a string in MySQL Jul 25, 2023 pm 01:18 PM

MySQL is a commonly used relational database management system that provides a variety of functions to process and operate data. Among them, the REPLACE function is used to replace the specified part of the string. In this article, we will introduce how to use the REPLACE function for string replacement in MySQL and demonstrate its usage through code examples. First, let’s take a look at the syntax of the REPLACE function: REPLACE(str,search_str,replace_str).

Can PHP replace the functionality of JSP? Can PHP replace the functionality of JSP? Mar 20, 2024 pm 03:45 PM

Can PHP replace the functionality of JSP? As web development technology continues to evolve, developers are often faced with choosing the appropriate server-side language to implement their project needs. In this regard, PHP and JSP are two common choices. JSP is the abbreviation of JavaServerPages, which is a server-side technology based on Java, while PHP is a server-side scripting language. This article will explore whether PHP can replace the functions of JSP and provide some specific code examples to help readers better understand

See all articles