I need some guidance on how to reverse a sentence and then return to main. Either way is fine, whether the words are reversed such as "Mot am I" (I am Tom) or "Tom am I".
The user will enter any sentence with a 6 word max. Then it will be reversed. Should I use .Split or ToCharArray? Here is what I have so far.
public static string Backwards() // Create Backwards Method
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Create a sentence with at least 6 words");
string userSentence = Console.ReadLine();
if (userSentence.Length <= 6)
{
}
}
Although I am pretty sure what I have isn't saying 6 or less words, it is saying 6 individual elements. Tips on how to limit a string to a certain amount of words? A lot of what I've searched has some more advanced concepts I don't quite understand yet. Any help is appreciated.
This might do the trick for you
public static string Backwards() // Create Backwards Method
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Create a sentence with at least 6 words");
string userSentence = Console.ReadLine();
//To count the number of words used split.length
if(userSentence.Split(' ').Length <= 6)
{
userSentence = String.Join(" ", userSentence.Split(' ').Reverse());
}
return userSentence;
}
The string.Join method combines many strings into one. It receives two arguments: an array (or IEnumerable) and a separator string.
Splits a string into substrings that are based on the characters in an array.
how would I return to main method?
private static void Main(string[] args)
{
string dorev = Backwards();
}