A program that will get an input string and provide the possible unique substrings of that string.
Ususally when a programmer goes to the interview, the company asks to write the code of some program to check your logic and coding abilities.
You can write the code in any language like C, C++, Java, C# and so on. There is no barrier to the language and technology because the company wants to check your ability to think and your skills to write the code.
Some companies only want the algorithm to check your skills.
One of the programs in those common programs is the following.
Write a function that will take a string A as input and print all the possible unique substrings present in a given string A.
Example
Input = ”abcde”
Output = a, ab, abc, abcd, abcde, b, bc, bcd, bcde, c, cd, cde, d, de, e
Input = ”hello”
Output = h, he, hel, hell, hello, e, el, ell, ello, l, ll, llo, lo, o
Input = ” abab”
Output = a, ab, aba, abab, b, ba, bab
To do this, I will use the Visual Studio 2012 as an IDE and C# as a language,
Create a new console application named "ConsoleApplication1".
Where you will get the page that will look like:
Now I will write the code in some steps.
Get the input string from the user.
- Console.Write("Eneter the input String:");
-
- string input = Console.ReadLine();
Write the code in a function to get all the possible unique substrings from the input string.
- public static void PossibleUniqueSubString(string input)
- {
-
- string final = "";
-
-
- for (int i = 0; i < input.Length; i++)
- {
- string str = "";
- for (int j = i; j < input.Length; j++)
- {
- str += input[j];
- final += str + ",";
- }
- }
-
-
- final = final.Remove(final.Length - 1, 1);
-
-
- string[] arr = final.Split(',');
-
-
- arr = arr.Distinct().ToArray();
-
-
- for (int i = 0; i < arr.Length; i++)
- Console.Write(arr[i]+" ");
- }
Call the function with the parameter of input string in the main() method as in the following:
- Console.Write("Eneter the input String:");
-
-
- string input = Console.ReadLine();
- PossibleUniqueSubString(input);
If I provide the ”abcde” as an input string then the output will be like:
a, ab, abc, abcd, abcde, b, bc, bcd, bcde, c, cd, cde, d, de, e
If I provide ”hello” as an input string then the output will be like:
h, he, hel, hell, hello, e, el, ell, ello, l, ll, llo, lo, o
If I provide ”abab” as an input string then the output will be like:
a, ab, aba, abab, b, ba, bab