Search code examples
c#stringpi

Count number before search query is found


I am trying to determine how far I have to go in pi to find a user-input search query. I attempted to use various properties of system.length, but I was unable to get was I was wanting. I essentially want to be able to enter a set of numbers, and have the console return how many numbers into pi the query was found. The delimiter is PiClass.CalculatePi is just to make sure it does not run forever.

    Console.WriteLine("type string to search");

    string searchForThis = Console.ReadLine();

    var PiClass = new PiClass();
    double.TryParse(PiClass.CalculatePi(3), out double pi);

    string piString = pi.ToString();

    if (piString.Contains(searchForThis) == true)
    {
        Console.WriteLine("Located");
    }
    else
    {
        Console.WriteLine("Please expend search");
    }

    Console.Read();

Solution

  • Here ya go:

            Console.WriteLine("type string to search");
        string searchForThis = Console.ReadLine();
        var PiClass = new PiClass();
        double.TryParse(PiClass.CalculatePi(3), out double pi);
        string piString = pi.ToString();
        int location = piString.IndexOf(searchForThis);
        if (location >=0)
        {
            Console.WriteLine("Located  at index: " + location.ToString());
        }
        else
        {
            Console.WriteLine("Please expend search");
        }
        Console.Read();