# Find indices of all occurrence of one string in other

Given two strings, str1 and str2, the task is to print the indices(Consider, indices starting from 0) of occurrence of str2 in str1. If no such index occurs, print “NONE”.

Examples:

```Input : GeeksforGeeks
Geeks
Output : 0 8

Input : GFG
g
Output : NONE
```

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

A simple solution is to check all substrings of given string one by one. If a substring matches, print its index.

## C++

 `// C++ program to find indices of all ` `// occurrences of one string in other. ` `#include ` `using` `namespace` `std; ` `void` `printIndex(string str, string s) ` `{ ` ` `  `    ``bool` `flag = ``false``; ` `    ``for` `(``int` `i = 0; i < str.length(); i++) { ` `        ``if` `(str.substr(i, s.length()) == s) { ` `            ``cout << i << ``" "``; ` `            ``flag = ``true``; ` `        ``} ` `    ``} ` ` `  `    ``if` `(flag == ``false``) ` `        ``cout << ``"NONE"``; ` `} ` `int` `main() ` `{ ` `    ``string str1 = ``"GeeksforGeeks"``; ` `    ``string str2 = ``"Geeks"``; ` `    ``printIndex(str1, str2); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find indices of all ` `// occurrences of one String in other. ` `class` `GFG { ` ` `  `    ``static` `void` `printIndex(String str, String s) ` `    ``{ ` ` `  `        ``boolean` `flag = ``false``; ` `        ``for` `(``int` `i = ``0``; i < str.length() - s.length() + ``1``; i++) { ` `            ``if` `(str.substring(i, i + s.length()).equals(s)) { ` `                ``System.out.print(i + ``" "``); ` `                ``flag = ``true``; ` `            ``} ` `        ``} ` ` `  `        ``if` `(flag == ``false``) { ` `            ``System.out.println(``"NONE"``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String str1 = ``"GeeksforGeeks"``; ` `        ``String str2 = ``"Geeks"``; ` `        ``printIndex(str1, str2); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-JI `

## Python3

 `# Python program to find indices of all ` `# occurrences of one String in other. ` `def` `printIndex(``str``, s): ` ` `  `    ``flag ``=` `False``; ` `    ``for` `i ``in` `range``(``len``(``str``)): ` `        ``if` `(``str``[i:i ``+` `len``(s)] ``=``=` `s): ` `             `  `            ``print``( i, end ``=``" "``); ` `            ``flag ``=` `True``; ` ` `  `    ``if` `(flag ``=``=` `False``): ` `        ``print``(``"NONE"``); ` `         `  `# Driver code         ` `str1 ``=` `"GeeksforGeeks"``; ` `str2 ``=` `"Geeks"``; ` `printIndex(str1, str2); ` ` `  `# This code contributed by PrinciRaj1992  ` ` `

## C#

 `// C# program to find indices of all ` `// occurrences of one String in other. ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``static` `void` `printIndex(String str, String s) ` `    ``{ ` ` `  `        ``bool` `flag = ``false``; ` `        ``for` `(``int` `i = 0; i < str.Length - s.Length + 1; i++) { ` `            ``if` `(str.Substring(i, ` `                              ``s.Length) ` `                    ``.Equals(s)) { ` `                ``Console.Write(i + ``" "``); ` `                ``flag = ``true``; ` `            ``} ` `        ``} ` ` `  `        ``if` `(flag == ``false``) { ` `            ``Console.WriteLine(``"NONE"``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{ ` `        ``String str1 = ``"GeeksforGeeks"``; ` `        ``String str2 = ``"Geeks"``; ` `        ``printIndex(str1, str2); ` `    ``} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## PHP

 ` `

Output:

```0 8
```

Time Complexity : O(n * n)

An efficient solution is to KMP string matching algorithm.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up 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.