-2

I want to write a program using C# reads a file and spits out another file that has all the words in original file in reverse order and is stripped of all words like "a" and "the".

using (FileStream stream = File.OpenRead("C:\\file1.txt"))
using (FileStream writeStream = File.OpenWrite("D:\\file2.txt"))
{
    BinaryReader reader = new BinaryReader(stream);
    BinaryWriter writer = new BinaryWriter(writeStream);

    // create a buffer to hold the bytes 
    byte[] buffer = new Byte[1024];
    int bytesRead;

    // while the read method returns bytes
    // keep writing them to the output stream
    while ((bytesRead =
            stream.Read(buffer, 0, 1024)) > 0)
    {
        writeStream.Write(buffer, 0, bytesRead);
    }
}

I've implemented the previous code. How reverse and spit out characters "a" and "the".

Sido rav
  • 23
  • 2

1 Answers1

5

File have static helpers that deal with reading and writing text - should be enough for most real cases where text is somewhat small:

File.WriteAllText(destinationFilePath,
String.Join(" ",
  File.ReadAllText(sourceFilePath)
   .Split(' ')
   .Where(s=> s != "a" && s != "the").Reverse())
);

If your source contains sentences and not just words separate by spaces - use regex to tokenize the text - Regex split string but keep separators instead of String.Split.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179