Reverse words in a string: Difference between revisions

Content deleted Content added
Cbrt74088 (talk | contribs)
Simplified C#
Line 720:
public class ReverseWordsInString
{
public static void Main(string[] args)
{
string[] linestext = {@"
"---------- Ice and Fire ------------",
" ",
"fire, in end will world the say Some",
"ice. in say Some ",
"desire of tasted I've what From ",
"fire. favor who those with hold I ",
" ",
"... elided paragraph last ... ",
" ",
"Frost Robert -----------------------" };
foreach(string line in lines)
{
string[] words = line.Split(' ');
Array.Reverse(words);
 
foreach(string wordfire, in words)end will world the say Some
{ice. in say Some
"desire of tasted I've what From ",
//String.Split() adds empty strings to the array. We must check
fire. favor who those if(word.Length !=with 0)hold I
 
Console.Write(word + " ");
}... elided paragraph last ...
 
Console.WriteLine();
"Frost Robert -----------------------" };
";
 
foreach (string line in linestext.Split(Environment.NewLine)) {
//Splits on any whitespace, not just spaces
string[] words = line.Split('default(char[]), 'StringSplitOptions.RemoveEmptyEntries);
Array.Reverse(words);
Console.WriteLine(string.Join(" ", words));
}
}