Find subString in String

Hi All,
I have written the below program to find the substring in the string. in case one word coming multiple time how to handle this situation ? the below program retuning -1 as position which is wrong.
Code
  1. Input ("abhijit" , "jit")

  2. public int FindSubString(string strSuper, string strSub)

  3.         {

  4.             char[] charSuper = strSuper.ToCharArray();

  5.             char[] charSub = strSub.ToCharArray();

  6.  

  7.             //int position = -1;

  8.             for (int i = 0; i <= charSuper.Length - charSub.Length; i++)

  9.             {

  10.                 int counter = 0;

  11.                 for (int J = 0; J < charSub.Length; J++)

  12.                 {

  13.                     if (charSuper[i] != charSub[J])

  14.                         continue;

  15.                     else

  16.                     {

  17.                         i++;

  18.                         counter++;

  19.                     }

  20.                 }

  21.                 if (counter == charSub.Length)

  22.                     position = i - charSub.Length;

  23.             }

  24.             return position;            

  25.         }
Copyright GeekInterview.com

How to fixi it ?

Questions by yrs_abhishek

Showing Answers 1 - 9 of 9 Answers

Kanwarjeet Yadav

  • Aug 7th, 2013
 

Find Substring in string

Code
  1. public static bool substr(string mainstr, string str)

  2.         {

  3.            

  4.             char[] main = mainstr.ToCharArray();

  5.             char[] sub = str.ToCharArray();

  6.             int j = 0; int k = 0;

  7.             for (int i = 0; i < sub.Length; i++)

  8.             {

  9.                 for (j = j; j < main.Length; j++)

  10.                 {

  11.                     if (main[j] == sub[i])

  12.                     {

  13.                         j++; k++;

  14.                         break;

  15.                     }

  16.                     else

  17.                     {

  18.                         if (k != 0)

  19.                         {

  20.                             i = 0; j -= 1;                          

  21.                         }

  22.                         k = 0;

  23.                     }

  24.                 }

  25.                

  26.             }

  27.             if (k == sub.Length)

  28.             return true;

  29.             else

  30.                 return false;

  31.  

  32.         }

  Was this answer useful?  Yes

Danthe74

  • Dec 14th, 2014
 

My solution require only one while loop.
Function returns the position of find chars.

Code
  1.         public static int FindSubString(string strSuper, string strSub)

  2.         {

  3.             char[] charSuper = strSuper.ToCharArray();

  4.             char[] charSub = strSub.ToCharArray();

  5.            

  6.             int position = -1;  // Marker for Position

  7.             int i = 0;          // Loop charSuper

  8.             int j = 0;          // Loop charSub

  9.             int counter = 0;    // Count number of find chars in charSuper

  10.  

  11.             if (charSuper.Length <= charSub.Length)

  12.             {

  13.                 // charSuper is less longer than charSub

  14.                 return position;

  15.             }

  16.  

  17.             while (i <= charSuper.Length - 1 || j <= charSub.Length - 1)

  18.             {

  19.                 if (strSuper[i] == strSub[j])

  20.                 {

  21.                     if (position == -1)

  22.                     {

  23.                         position = i + 1;

  24.                     }

  25.                     i++;

  26.                     j++;

  27.                     counter++;

  28.                 }

  29.                 else

  30.                 {

  31.                     position = -1;

  32.                     j = 0;

  33.                     counter = 0;

  34.                     i++;

  35.                 }

  36.             }

  37.  

  38.             if (counter != strSub.Length)

  39.             {

  40.                 position = -1;

  41.             }

  42.  

  43.             return position;

  44.         }

  Was this answer useful?  Yes

Annon

  • Jun 15th, 2016
 

This answer is wrong. Ex:
substr("abcabcabcd", "abcabcd")
=> false (should return true)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions