To Reverse words in a full of sentence first you would need to split that sentence by space(' ') and create list of strings and push each word by word in to that list. Then by decrementing the index value of list iterate through the list. Append each string to string builder class and print that string builder object. That's it your done ..!!!
Class Program
{
static void Main(string[] args)
{
string sample = "My name is string";
List<string> smaplestrnigs = new List<string>();
smaplestrnigs = sample.Split(' ').ToList();
StringBuilder str = new StringBuilder();
for (int i = smaplestrnigs.Count-1; i > -1 ; i-- )
{
str.Append(smaplestrnigs[i]);
str.Append(" ");
}
Console.WriteLine(str.ToString());
}
}
Output: string is name My
No comments:
Post a Comment