- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count of sub-strings of length n possible from the given string in C++
We are given a string str[] and a number n. The goal is to find all substrings of str[] that have length n. If string is “abcde” and n=3 then substrings of length 3 are “abc”, “bcd”, “cde” and count is 3.
Let us understand with examples.
Input − str[] = “computer” n=4
Output − Count of substrings of length n possible from the given string are − 5
Explanation − Substrings with length 4 are: “comp”, “ompu”,”mput”, “pute”, “uter”
Input − str[] = “development” n=5
Output − Count of substrings of length n possible from the given string are − 7
Explanation − Substrings with length 5 are: “devel”, “evelo”, “velop”, “elopm”, “lopme”, “opmen”, “pment”.
Approach used in the below program is as follows
If we take the length of string str[] as L then the count of substrings of length n inside str[] is Ln+1. If string is “abcdefghi” and n is 4 then substrings will be “abcd”, “bcde”, “cdef”, “defg”, “efgh”, “fghi”. Count is 6. Also 9-4+1=6.
Take a string str.
Take n as integer.
Function possible_substring(string str, int length, int n) takes a string, it’s length, n and returns the count of substrings of str with length n.
Take a variable count.
Set count = length-n+1.
Return count as result at the end.
Example
#include <bits/stdc++.h> using namespace std; int possible_substring(string str, int length, int n){ int count = length - n + 1; return count; } int main(){ string str = "learning"; int length = str.length(); int n = 2; cout<<"Count of substrings of length n possible from the given string are: "<<possible_substring(str, length, n); return 0; }
Output
If we run the above code it will generate the following output −
Count of substrings of length n possible from the given string are: 7
- Related Articles
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- All possible strings of any length that can be formed from a given string?
- Count all Palindrome Sub-Strings in a String in C++
- Print all possible strings of length k that can be formed from a set of n characters in C++
- The k-th Lexicographical String of All Happy Strings of Length n in C++
- Count subsequence of length three in a given string in C++
- Find all distinct palindromic sub-strings of a given String in Python
- Find all palindromic sub-strings of a given string - Set 2 in Python
- Maximum trace possible for any sub-matrix of the given matrix in C++
- Count of sub-strings that contain character X at least once in C++
- Count the number of common divisors of the given strings in C++
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Find the count of sub-strings whose characters can be rearranged to form the given word in Python
- Count all possible N digit numbers that satisfy the given condition in C++
- Count Possible Decodings of a given Digit Sequence in C++
