Answer to Question #264215 in C# for Tori

Question #264215

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.


1
Expert's answer
2021-11-11T07:30:45-0500

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);

    }

  }

}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS