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

  • برمجة سي شارب
  • برمجة

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

الأجوبة

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;
        }
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...