-1

I'm trying to make a program that build a string of words and then find those words in the same string.

The first half of the question is working but I'm having problems with the second. The program finds the words, the first time and the last but, how do I found a eventual middle one? And then, how do I count them?

        string strfinal;
        string frase = "", texto = "";
        string textoAEncontrar;
        Console.WriteLine("Insira algum texto (carregue no * para terminar a escrita): ");

        for (texto = ""; !texto.Equals("*");)
        {
            texto = Console.ReadLine();
            if (texto.Length < 100)
            {
                frase = frase + " " + texto;
            }
        }


        strfinal = frase.Substring(1, frase.Length - 2);

        Console.WriteLine(strfinal);

        Console.WriteLine("O que deseja encontrar no texto escrito: ");
        textoAEncontrar = Console.ReadLine();
        int primeiraReferenciaNoTexto = strfinal.IndexOf(textoAEncontrar);
        int ultimaReferenciaNoTexto = strfinal.LastIndexOf(textoAEncontrar);


        if (strfinal.Contains(textoAEncontrar))
            {

                Console.WriteLine("A palavra {0} existe no index {1}", textoAEncontrar, primeiraReferenciaNoTexto);
                Console.WriteLine("A palavra {0} existe no index {1}", textoAEncontrar, ultimaReferenciaNoTexto);
            }
            else
            {
                Console.WriteLine("A palavra {0} não existe", textoAEncontrar);
            }



    }
}
Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34
Bruno Dias
  • 3
  • 1
  • 4

3 Answers3

1

You need to chain your IndexOf calls, like so:

var i = -1
while (true)
{
     i = strFinal.IndexOf(textoAEncontrar, i+1);
     if (i == -1) break;
     Console.WriteLine("Found string at {0}", i);
}

You may need to improve the boundary checks above but this is the general idea.

John Wu
  • 50,556
  • 8
  • 44
  • 80
0

at a time like this, RegEx is what you would definitely need. RegEx has a .Match which just might be what you need.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        Regex regex = new Regex(@"\d+");
        Match match = regex.Match("Dot 55 Perls");
        if (match.Success)
        {
            Console.WriteLine(match.Value);
        }
    }
}

since we are looking for numerical values from a string, we will get 55

Refer to the links below for further explanation on this:

http://www.dotnetperls.com/regex

https://stackoverflow.com/a/2159085/5694113

Community
  • 1
  • 1
TheQuestioner
  • 702
  • 10
  • 28
0

You can create a method that gets the index of the nth occurrence of a word in a string. You can also use text.Split to count the number of occurrence since you have a delimiter char space.

        static void Main(string[] args)
        {    
            string text = "apple cinder apple goat apple";
            string searchWord = "apple";

            string[] textSplit = text.Split(' ');

            int searchResultCount = textSplit.Where(s => s == searchWord).Count();

            Console.WriteLine(text);
            Console.WriteLine(searchWord);
            Console.WriteLine(searchResultCount);
            Console.WriteLine(IndexOfOccurence(text, searchWord, 2));

            Console.ReadLine();
        }

        static int IndexOfOccurence(string s, string match, int occurence)
        {
            int i = 1;
            int index = 0;

            while (i <= occurence && (index = s.IndexOf(match, index)) != -1)
            {
                if (i == occurence)
                   return index;
                index++;
                i++;
            }

            return -1;
        }
jegtugado
  • 5,081
  • 1
  • 12
  • 35