# Count of sub-strings of length n possible from the given string

Given a string str and an integer N, the task is to find the number of possible sub-strings of length N.

Examples:

Input: str = “geeksforgeeks”, n = 5
Output: 9
All possible sub-strings of length 5 are “geeks”, “eeksf”, “eksfo”,
“ksfor”, “sforg”, “forge”, “orgee”, “rgeek” and “geeks”.

Input: str = “jgec”, N = 2
Output: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The count of sub-strings of length n will always be len – n + 1 where len is the length of the given string. For example, if str = “geeksforgeeks” and n = 5 then the count of sub-strings having length 5 will be “geeks”, “eeksf”, “eksfo”, “ksfor”, “sforg”, “forge”, “orgee”, “rgeek” and “geeks” which is len – n + 1 = 13 – 5 + 1 = 9.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of ` `// possible sub-strings of length n ` `int` `countSubStr(string str, ``int` `n) ` `{ ` `    ``int` `len = str.length(); ` `    ``return` `(len - n + 1); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"geeksforgeeks"``; ` `    ``int` `n = 5; ` ` `  `    ``cout << countSubStr(str, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*;  ` ` `  `class` `GFG ` `{ ` ` `  `// Function to return the count of ` `// possible sub-strings of length n ` `static` `int` `countSubStr(String str, ``int` `n) ` `{ ` `    ``int` `len = str.length(); ` `    ``return` `(len - n + ``1``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[])  ` `{ ` `    ``String str = ``"geeksforgeeks"``; ` `    ``int` `n = ``5``; ` ` `  `    ``System.out.print(countSubStr(str, n)); ` `} ` `} ` ` `  `// This code is contributed by mohit kumar 29 `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to return the count of  ` `# possible sub-strings of length n  ` `def` `countSubStr(string, n) :  ` ` `  `    ``length ``=` `len``(string);  ` `    ``return` `(length ``-` `n ``+` `1``);  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``string ``=` `"geeksforgeeks"``;  ` `    ``n ``=` `5``;  ` ` `  `    ``print``(countSubStr(string, n)); ` `     `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to return the count of ` `// possible sub-strings of length n ` `static` `int` `countSubStr(``string` `str, ``int` `n) ` `{ ` `    ``int` `len = str.Length; ` `    ``return` `(len - n + 1); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main()  ` `{ ` `    ``string` `str = ``"geeksforgeeks"``; ` `    ``int` `n = 5; ` ` `  `    ``Console.WriteLine(countSubStr(str, n)); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech. `

## PHP

 ` `

Output:

```9
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.