How to Match a Substring From a String

Introduction

A program that will get a string and match it with a substring.

Usually when a programmer goes to an 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 required 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.

Question

You are given an input string, string1. Write a function to find whether string1 contains all the characters of the word “nagarro” in the same order as they appear in “nagarro”. The function will be true if the input string1 contains all the letters in the same order and false otherwise.

For example:

Input- string1= “nbacgddardsardo”
Output: True (explanation: “nbacgddardsardo”)

Input- string1= “anagarorpa”
Output: False (explanation: “anagarorpa”)

Input- string1= “nagarro01”
Output: True (explanation: “nagarro01”)

To do this, I will use Visual Studio 2012 as an IDE and C# as the language.

Create a new console application named "ConsoleApplication1".



Where you will get the page that will look like:



I will now provide the procedure for creating the code.
  1. Add the code in the main() method to get some inputs from the user.

       - Get the string
       - Get the sub string to match  
    1. static void Main(string[] args)  
    2. {  
    3.     //Get the string from the user  
    4.     Console.Write("Eneter the string:");  
    5.     string input = Console.ReadLine();  
    6.   
    7.     //Get the sub string from the user  
    8.     Console.Write("Eneter the sub string:");  
    9.     string match = Console.ReadLine();          
    10. }  


  2. Write the code in a function to match the sub-string within the input string and return True/False.
    1. private static bool substr(string input, string match)   
    2. {  
    3.     //Create temp string  
    4.     string temp = "";  
    5.   
    6.     for (int i = 0, k = 0; i < input.Length; i++)   
    7.     {  
    8.         //Match the value char by char of both string  
    9.         if (input[i] == match[k])   
    10.         {  
    11.             //if matches then add the char to temp variable  
    12.             temp += input[i];  
    13.             k++;  
    14.         }  
    15.         //If matching string completed  
    16.         if (k == match.Length) break;  
    17.     }  
    18.     //return the true/false on basis of matching  
    19.     return match.Equals(temp);  
    20. }  


  3. Call the function with the 2 parameters, array and sum, from the main() method.
    1. Console.WriteLine("Output String is:");  
    2. //Call the function and print its return value  
    3. Console.WriteLine(substr(input, match).ToString());  
    4. Console.ReadKey();  

Result

  1. If I provide ”nbacgddardsardo” as the string and “nagarro” as the sub-string then the output will be:

    Output: True



  2. If I provide ”anagarorpa” as the string and “nagarro” as the sub-string then the output will be like:

    Output: False



  3. If I provide ”nagarro01” as the string and “nagarro” as the sub-string then the output will be like:

    Output: True

Up Next
    Ebook Download
    View all
    Learn
    View all