Answer to Question #273684 in C# for Holik

Question #273684

1. Change the value of "0" in the input file to "1" and the value of "1" to "0" and write to the output file

1
Expert's answer
2021-12-03T12:37:46-0500
using System;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // input file: fill the file 'in.txt' with data (in folder bin\debug)
            const string fileNameIn = "in.txt";
            // output file
            const string fileNameOut = "out.txt";

            try
            {
                // read data from file
                string fileContentsIn = System.IO.File.ReadAllText(fileNameIn);
                StringBuilder fileContentsOut = new StringBuilder(fileContentsIn);
                // replace "0" to "1" and "1" to "0"
                for (int i = 0; i < fileContentsOut.Length; i++)
                {
                    if (fileContentsOut[i] == '0')
                    {
                        fileContentsOut[i] = '1';
                    }
                    else if (fileContentsOut[i] == '1')
                    {
                        fileContentsOut[i] = '0';
                    }
                }
                // write output file
                System.IO.File.WriteAllText(fileNameOut, fileContentsOut.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Error:" + e.Message);
                return;
            }
        }
    }
}

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
New on Blog
APPROVED BY CLIENTS