Write a program that can
1. Write a sentence backwards
2. Write a sentence downwards, 1 letter at a time
3. Write a sentence upwards, 1 letter at a time.
using System;
using System.Linq;
namespace Q264215
{
// Write a program that can
//1. Write a sentence backwards
//2. Write a sentence downwards, 1 letter at a time
//3. Write a sentence upwards, 1 letter at a time.
class Program
{
static void Main(string[] args)
{
var originalSentence = "The dish ran away with the spoon";
Console.WriteLine("The original sentence reads:");
Console.WriteLine(originalSentence);
// Convert string to array of words and reverse the order
var backwardsSentence = String.Join(" ", originalSentence.Split(' ').Reverse());
Console.WriteLine("\nThe original sentence written backwards reads:");
Console.WriteLine(backwardsSentence);
Console.WriteLine("\nThe original sentence written downwards reads:");
//Iterate over each char in sentence and output it
foreach (var c in originalSentence)
{
Console.WriteLine(c);
}
//reverse the sentence
var upwardsSentence = ReverseString(originalSentence);
Console.WriteLine("\nThe original sentence written upwards reads:");
//Iterate over each char in sentence and output it
foreach (var c in upwardsSentence)
{
Console.WriteLine(c);
}
}
/// <summary>
/// Utility method to reverse a string
/// </summary>
/// <param name="s"></param>
/// <returns>the reversed string</returns>
private static string ReverseString(string s)
{
//convert string to char array
char[] charArray = s.ToCharArray();
//call array reverse method to reverse the array
Array.Reverse(charArray);
//cast array to a string and return it
return new string(charArray);
}
}
}
Comments
Leave a comment