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: 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?

Test your Programming skills with w3resource's quiz.