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 :
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework