Find if the characters of the sample string is in the same order in the text string – Nagarro Interview Question with Answer

This is another Nagarro Interview Question. In this particular case You are given 2 input Strings a text string and a sample string, you are supposed to find out whether the characters of sample string is in the same order in the text string or not.

OrderText




using System;
namespace OrderStringDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "ACNSGDFSGAHHNPKSGRKAULKRDFRHLKOLKA";
            string sample = "NAGARRO";
            Console.WriteLine("Text string   : " + text);
            Console.WriteLine("Sample string : " + sample);
           
            
            int i = 0;
            int j = 0;
            int flag = 0;

            do
            {
                if (sample[i] == text[j])
                {
                    if (i == sample.Length - 1)
                    {
                        flag = 1;
                        Console.WriteLine("Output : Yes");
                    }
                     

                    i++;
                    j++;
                }
                else
                    j++;

            }
            while (i < sample.Length && j < text.Length);

            if(flag==0)
                Console.WriteLine("Output : NO");
            Console.ReadKey();
        }

    }
}