1

I'm trying to find an algorithm that takes an input from user (string1) and then takes another (string2) then counts How many string2 exist in string1. for example:

there is 2 # in string1 here

string1="##"
string2="#"
answer:2

there is 2 ## in string1:(1st one with the index of 0,1 and 2nd one with the index of 1,2)

string1="###"
string2="##"
answer=2

there is 3 ## in string1:(1st:index of 0,1 2nd:index of 1,2 3rd:index of 2,3)

string1="####"
string2="##"
answer=3

for counting # here we can simply do this:

string1.Length - string2.Length + 1

but I can only make the algorithm work for examples like these, while inputs can be anything. now i need an algorithm to do this for me. Here is ,y code that works for my inputs, but doesn't work for all inputs. for example if string2 was #a.

string string1 = "#abc##asd###12####";
string string2 = "##";
char[] str2 = string2.ToCharArray();
string[] str1 = string1.Split(str2);
string chars = "";
foreach (string s in str1)
    foreach (char c in s)
        chars += c;
//Result for chars are:a, b, c, a, s, d, 1, 2 
string[] splits = string1.Split(chars.ToCharArray());
//Result for splits are:#, ##, ###, ####
int sum = 0;
for (int i = 0; i < splits.Length; i++)
{
    if (splits[i].Length<string2.Length)
        continue;
    else
        sum += splits[i].Length - string2.Length + 1;
}
Console.WriteLine(sum);

In my algorithm i seprated string1 by string2 characters and then count strings[] that was created by split method.

I already know that my algorithm is completely wrong, but I couldn't solve it anyway. I just put the code to show what i have tried.
if you write algorithm in your answer, that would be nice. I also have a little knowledge about lambda and linq so no problem if you could to this by lambda or linq or func or expression but explain about it.

  • This is IMHO a duplicate of this: http://stackoverflow.com/questions/541954/how-would-you-count-occurrences-of-a-string-within-a-string – BCdotWEB Oct 15 '14 at 15:06
  • 2
    @BCdotNET your link isn't my answer at all. here inputs can be anything while, in your link it is an specific input. answer if you have ways to solve the problem. –  Oct 15 '14 at 15:10
  • `Regex.Matches(string1, string2).Count` – Arian Motamedi Oct 15 '14 at 15:16
  • got -1 less that 2 minutes, how could he/she read the question so fast???!!! –  Oct 15 '14 at 15:16
  • 1
    @PoweredByOrange wrong answer. try string1:#### and string2:### answer must be 2 but your code gives 1 –  Oct 15 '14 at 15:18
  • @masoudmohammadi How would that be two? Do you see two `###` in `####`? – Arian Motamedi Oct 15 '14 at 15:37
  • 2
    yes, you haven't read my examples carefully. there is 2, 1st one with the index of 0,1,2 and 2nd one with the index of 1,2,3. this is ###3 one of them and this is 0### another one.Understand that? –  Oct 15 '14 at 15:39

1 Answers1

2

Here's a working code :

string string1 = "#abc##asd###12####";
string string2 = "##";
List<int> indexes = new List<int>();
int index = -1;
while((index = string1.IndexOf(string2, index + 1)) >= 0)
{
    indexes.Add(index);
}

Console.WriteLine(indexes.Count);
Masoud Mohammadi
  • 1,721
  • 1
  • 23
  • 41
luke77
  • 221
  • 3
  • 15
  • wrong answer. try string1:#### and string2:### answer must be 2 but your code gives 0 –  Oct 15 '14 at 15:14
  • I think its very close. Just an off by one error. [Check the fiddle](https://dotnetfiddle.net/4e45gq) The code gives 6, but the answer should be 7 – crthompson Oct 15 '14 at 15:16
  • Ok, my mistake, a = is missing in the while condition. I added it. – luke77 Oct 15 '14 at 15:17
  • Excellent work, if @masoudmohammadi doesnt think thats the right answer, then there is not enough information. Oh.. and yes.. its 6 not 7.. :) – crthompson Oct 15 '14 at 15:19
  • @paqogomez I'm testing it with other inputs. –  Oct 15 '14 at 15:23
  • works fine,can you explain about while condition, i can +1 your answer because i don't have 15 rep yet. –  Oct 15 '14 at 15:36
  • @luke77 yes, i could. i did now, but i was waiting for while condition explanation.! –  Oct 15 '14 at 15:55