- Course
- Text Files B
- Reverse text from a file
Reverse text from a file
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that makes a copy of a text file but reverse all its content. Create a file with the same name but with extension "inv".
Input
Output
Solution
using System.IO;
public class ReverseTextFile
{
public static void Main(string[] args)
{
string inputFileName = "input.txt";
string outputFileName = "input.inv";
string[] lines = File.ReadAllLines(inputFileName);
using (StreamWriter writer = File.CreateText(outputFileName))
{
for (int i = lines.Length - 1; i >= 0; i--)
{
writer.WriteLine(lines[i]);
}
}
}
}