Q:

Write a C# Sharp program to find the position of a specified word in a given string

0

Write a C# Sharp program to find the position of a specified word in a given string. 

Sample Example:
Text: The quick brown fox jumps over the lazy dog.
Position: 1 2 3 4 5 6 7 8 9

Expected Output :

Original string: The quick brown fox jumps over the lazy dog.
Position of the word 'fox' in the said string: 4
Position of the word 'The' in the said string: 1
Position of the word 'lazy' in the said string: 8

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "The quick brown fox jumps over the lazy dog.";
            Console.WriteLine("Original string: " + str1);
            Console.WriteLine("Position of the word 'fox' in the said string: " + test(str1, "fox"));
            Console.WriteLine("Position of the word 'The' in the said string: " + test(str1, "The"));
            Console.WriteLine("Position of the word 'lazy' in the said string: " + test(str1, "lazy"));
        }
        public static int test(string text, string word)
        {
            return Array.IndexOf(text.Split(' '), word)+1;
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now