41.6k views
2 votes
Assume that sentence is a variable of type String that has been assigned a value. Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared, firstWord, also of type String. Write the statements needed so that the first word of the value of sentence is assigned to firstWord. So, if the value of sentence were "Broccoli is delicious.", your code would assign the value "Broccoli" to firstWord.

User Qinmiao
by
4.6k points

1 Answer

3 votes

Answer:

//get the sentence from the user

Console.WriteLine("Enter your sentence");

//read the user information

string ans = Console.ReadLine();

//check if sentence ends with period

if(!ans.EndsWith("."))

{

Console.WriteLine("Sentence should end with period");

Environment.Exit(0);

}

//declear empty string firstword

string firstWord = "";

//split the requested sentence using single space character

string[] splitans = ans.Split(' ');

//assign firstword

firstWord = splitans[0];

//print out firstword

Console.WriteLine(firstWord);

Step-by-step explanation:

The program uses c#.

User Specur
by
5.3k points