Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Find the position of a specified word in a given string

C# Sharp String: Exercise-53 with Solution

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

Sample Solution:-

C# Sharp Code:

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;
        }
    }
}

Sample 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

Flowchart :

Flowchart: C# Sharp Exercises - Find the position of a specified word in a given string.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to convert the first character of each word of a given string to uppercase.
Next: Write a C# Sharp program to alternate the case of each letter in a given string and the first letter of the said string must be uppercase.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.