w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Check if a string starts with a specified word

C# Sharp Basic: Exercise-34 with Solution

Write a C# program to check if a string starts with a specified word.
Note: Suppose the sentence starts with "Hello"
Sample Data: string1 = "Hello how are you?"
Result: Hello.

C# Sharp Exercises: Check if a string starts with a specified word

Sample Solution:-

C# Sharp Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Exercise34 {
  static void Main(string[] args)
        {
           string str; 
           Console.Write("Input a string : ");
           str = Console.ReadLine();
           Console.WriteLine((str.Length < 6 && str.Equals("Hello")) || (str.StartsWith("Hello") && str[5] == ' '));
        }
}

Sample Output:

Input a string : Hello how are you?                                    
True

Flowchart:

Flowchart: C# Sharp Exercises - Check if a string starts with a specified word

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to check if a given positive number is a multiple of 3 or a multiple of 7.
Next: Write a C# program to check two given numbers where one is less than 100 and other is greater than 200.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming