# 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.
```CodeInput ("abhijit" , "jit")
public int FindSubString(string strSuper, string strSub)
{
char[] charSuper = strSuper.ToCharArray();
char[] charSub = strSub.ToCharArray();

//int position = -1;
for (int i = 0; i <= charSuper.Length - charSub.Length; i++)
{
int counter = 0;
for (int J = 0; J < charSub.Length; J++)
{
if (charSuper[i] != charSub[J])
continue;
else
{
i++;
counter++;
}
}
if (counter == charSub.Length)
position = i - charSub.Length;
}
return position;

How to fixi it ?

• Aug 7th, 2013

Find Substring in string

```Codepublic static bool substr(string mainstr, string str)
{

char[] main = mainstr.ToCharArray();
char[] sub = str.ToCharArray();
int j = 0; int k = 0;
for (int i = 0; i < sub.Length; i++)
{
for (j = j; j < main.Length; j++)
{
if (main[j] == sub[i])
{
j++; k++;
break;
}
else
{
if (k != 0)
{
i = 0; j -= 1;
}
k = 0;
}
}

}
if (k == sub.Length)
return true;
else
return false;

}```

#### Danthe74

• Dec 14th, 2014

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

```Code        public static int FindSubString(string strSuper, string strSub)
{
char[] charSuper = strSuper.ToCharArray();
char[] charSub = strSub.ToCharArray();

int position = -1;  // Marker for Position
int i = 0;          // Loop charSuper
int j = 0;          // Loop charSub
int counter = 0;    // Count number of find chars in charSuper

if (charSuper.Length <= charSub.Length)
{
// charSuper is less longer than charSub
return position;
}

while (i <= charSuper.Length - 1 || j <= charSub.Length - 1)
{
if (strSuper[i] == strSub[j])
{
if (position == -1)
{
position = i + 1;
}
i++;
j++;
counter++;
}
else
{
position = -1;
j = 0;
counter = 0;
i++;
}
}

if (counter != strSub.Length)
{
position = -1;
}

return position;
}```

#### Annon

• Jun 15th, 2016

substr("abcabcabcd", "abcabcd")
=> false (should return true)  