
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count the number of vowels occurring in all the substrings of given string in C++
Given a string str containing English alphabets. The goal is to find the number of vowels occurring in all the substrings of str. If string is “abcde” then substrings will be “a”, “b”, “c”, “d”, “e”, “ab”, “bc”, “cd”, “de”, “abc”, “bcd”, “cde”, “abcd”, “bcde”, “abcde”. The count of vowels in these substrings is 10. (a and e)
For Example
Input
str = ”aloe”
Output
Count the number of vowels occurring in all the substrings of given string are: 14
Explanation
The substrings are: “a”, “l”, “o”, “e”, “al”, “lo”, “oe”, “alo”, “loe”, “aloe”. Total vowels in these are: 14
Input
str=”http”
Output
Count the number of vowels occurring in all the substrings of given string are: 0
Explanation
The substrings are: “h”, “t”, “t”, “p”, “ht”, “tt”, “tp”, “htt”, “ttp”, “http”. Total vowels in these are: 0
Approach used in the below program is as follows −
In this approach we will create a vector vec, which stores counts of occurrences of ith character in all substrings in vec[i].
The 0th character occurs in n substrings where n is length of string str.
The ith character occurs in all substrings containing it ( n−i ) + number of substrings containing ith character as well as previous character ( arr[ i−1 ] ) − number of substrings formed by previous characters only ( i ).
Take a string str as input.
Function substring_vowels_count(string str, int length) takes str with its length and returns the count the number of vowels occurring in all the substrings of a given string.
Take the initial count as 0.
Take an integer vector vec.
Traverse vec using a for loop from i−0 to i<length and populate it with counts of occurrences of ith position character in all substrings of str.
If i=0, for 0th character this count is length. Set vec[0]=length using push_back[length].
For all other characters set using push_back(temp_1 + temp_2) where temp_1=length−1 and temp_2=vec[i−1]−i.
Now traverse str again using for loop and for each str[i] as vowel, ( a , e, i, o, or u ). Add vec[i] to count.
At the end we will have a total number of occurrences of vowels in substrings in count.
Return count as result.
Example
#include <bits/stdc++.h> using namespace std; int substring_vowels_count(string str, int length){ int count = 0; vector<int> vec; for (int i = 0; i < length; i++){ if (i == 0){ vec.push_back(length); } else { int temp_1 = length − i; int temp_2 = vec[i − 1] − i; vec.push_back(temp_1 + temp_2); } } for (int i = 0; i < length; i++){ if(str[i] == 'a' || str[i] == 'i' || str[i] == 'e' || str[i] == 'o' || str[i] == 'u'){ count = count + vec[i]; } } return count; } int main(){ string str = "honesty"; int length = str.length(); cout<<"Count the number of vowels occurring in all the substrings of given string are: "<<substring_vowels_count(str, length); return 0; }
Output
If we run the above code it will generate the following output −
Count the number of vowels occurring in all the substrings of given string are: 28
- Related Articles
- Count Unique Characters of All Substrings of a Given String in C++
- Count the pairs of vowels in the given string in C++
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Python program to count number of vowels using set in a given string
- Java program to count the number of vowels in a given sentence
- How to Count the Number of Vowels in a string using Python?
- Java Program to count all vowels in a string
- Program to find total sum of all substrings of a number given as string in Python
- Find substrings that contain all vowels in Python
- Program to print all substrings of a given string in C++
- C# Program to count number of Vowels and Consonants in a string
- C++ code to count number of even substrings of numeric string
- How to count number of vowels and consonants in a string in C Language?
- Count substrings with each character occurring at most k times in C++
