[BAEKJOON] 백준 6438: Reverse Text (C#)

2024. 6. 14. 02:16IT/BaekJoon

문제 링크

https://www.acmicpc.net/problem/6438

 

 

문제

In most languages, text is written from left to right. However, there are other languages where text is read and written from right to left. As a first step towards a program that automatically translates from a left-to-right language into a right-to-left language and back, you are to write a program that changes the direction of a given text.

 

 

입력

The input file contains several test cases. The first line contains an integer specifying the number of test cases in the file. Each test case consists of a single line of text which contains at most 70 characters. However, the newline character at the end of each line is not considered to be part of the line.

 

 

출력

For each test case, print a line containing the characters of the input line in reverse order.

 

 

 

 

통과한 답안

namespace _6438
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());

            for (int i = 0; i < T; i++)
            {
                string input = Console.ReadLine();
                string answer = new string(input.Reverse().ToArray());

                Console.WriteLine(answer);
            }
        }
    }
}

 

입력되는 문자열을 뒤집어서 출력하는 문제이다.

 

직접 뒤집는 방법은 여러가지가 있을 수 있으나,

시간과 메모리의 안정성을 위해서 내장된 Reverse() 메서드를 사용하였다.

 

만약 직접 구현해야 된다면

input에 길이에 해당하는 문자 배열을 생성하고 뒤에서부터 받아오게 한 후에

이를 string으로 연결시키는 방법을 사용할 수 있다.

namespace _6438
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int T = int.Parse(Console.ReadLine());

            for (int i = 0; i < T; i++)
            {
                string input = Console.ReadLine();
                char[] reverseInput = new char[input.Length];
                for (int j = 0; j < reverseInput.Length; j++)
                {
                    reverseInput[j] = input[reverseInput.Length - j - 1];
                }
                string answer = new string(reverseInput);
                Console.WriteLine(answer);
            }
        }
    }
}