Here we will work with two for loop to fulfill our requirements; let's see step by step.
Step 1
In this step first create a string variable, and ask user to provide input and assign it to created variable.
Step 2
Now in this step you need to create for loop to get characters one by one from the string.
Create a for loop to iterate our string character one by one. As you already know Length function returns the length of a string, to do so we will start our loop from index 0 to less than the length of the string.
Step 3
In this step, we will again need one more for loop to start from the value found with the help of the first loop. In loops, we created local variable in the first loop i and in the second loop j. In our case j always starts from the value of i from the first for loop so that none of the characters of the string are left.
Here we will use StringBuilder to store our result. We can use string, but in this case better to use StringBuilder for better performance. Hope you already know the difference between string and StringBuilder.
Step 4
In this step, we are iterating and storing the value in the newly created StringBuilder variable and appending the value to the same variable till our inner loop finish.
Check the below code.
using System;
using System.Text;
namespace ConsoleApp {
class FindSubString {
static void Main(string[] args) {
string str;
Console.WriteLine("Enter a string: ");
str = Console.ReadLine();
for (int i = 0; i < str.Length; i++) {
StringBuilder newString = new StringBuilder();
for (int j = i; j < str.Length; j++) {
newString.Append(str[j]);
Console.Write(newString + " ");
}
}
Console.ReadLine();
}
}
}
Output
Enter a string:
abcd
a ab abc abcd b bc bcd c cd d