- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the Number of Substrings of a String using C++

In this article, you will learn about the approaches to find the number of substrings (non-empty) that you can form in a given string.

Input : string = “moon” Output : 10 Explanation: Substrings are ‘m’, ‘o’, ‘o’, ‘n’, ‘mo’, ‘oo’, ‘on’, ‘moo’, ‘oon’ and ‘moon’. Input : string = “yellow” Output : 21

## Approach to find The Solution

Let the length of the string be n, so looking at the example above, we understand that to find all possible numbers of substrings, we need to add substrings of length n, (n-1), (n-2), (n-3), (n-4),......2, 1.

Total number of substrings = n + (n - 1) + (n - 2) + (n - 3) + (n - 4) + ……. + 2 + 1.

= n * (n + 1) / 2

So now we have a formula for evaluating the number of substrings where n is the length of a given string.

## C++ Code for the Above Approach

Here is the C++ syntax which we can use as an input to solve the given problem −

## Example

#include<bits/stdc++.h> using namespace std; int main () { string str = "yellow"; // finding the length of string int n = str.length (); // calculating result from formula int number_of_strings = n * (n + 1) / 2; cout << "Number of substrings of a string : " << number_of_strings; return 0; }

## Output

Number of substrings of a string: 21

## Explanation of the Code

This is the optimistic and straightforward approach of finding the number of possible substrings from a given string.

Firstly, in this code, we find the length of a given string from the .length() function and put that value in the formula we derived above, printing the output stored in the result variable.

## Conclusion

In this article, we have explained the way to find the number of substrings in a string where we first derive the formula for finding the number of all possible substrings and get the result using the formula from the length of the string. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this article helpful.

- Related Questions & Answers
- Find the Number of Substrings of One String Present in Other using C++
- Find all substrings in a string using C#
- Number of even substrings in a string of digits in C++
- Program to find split a string into the max number of unique substrings in Python
- Program to find number of different substrings of a string for different queries in Python
- Program to find number of substrings with only 1s using Python
- Program to find out number of distinct substrings in a given string in python
- Program to find total sum of all substrings of a number given as string in Python
- Find number of spaces in a string using JavaScript
- Rearrange the string to maximize the number of palindromic substrings in C++
- Number of Substrings divisible by 6 in a String of Integers in C++
- C++ code to count number of even substrings of numeric string
- Program to find out the number of pairs of equal substrings in Python
- Count the number of vowels occurring in all the substrings of given string in C++
- Is the string a combination of repeated substrings in JavaScript