FILE INCLUDED WITH ASSIGNMENT IS NAMED: InputRead2.txt Insiside this text docume
ID: 3850072 • Letter: F
Question
FILE INCLUDED WITH ASSIGNMENT IS NAMED: InputRead2.txt Insiside this text document is this: eafcdfghijlk Program we are using: C# with Visual Studios
Assignment:
Using the techniques you learned in the previous activity, develop code that will take input, line-by-line, from a file and write output to a file. The output file should have the characters alphabetized, line-by-line.
You will accomplish this by creating a method, “alphabetize”, which has the following method header:
//public static string alphabetize(string letters)
//Takes a string of letters passed to it, converts to an array of characters (use: ToCharArray()), uses Array.Sort to alphabetize, converts
//back to string (new string(char[])and returns the alphabetized version to calling program
Explanation / Answer
//below codes reads the file by line by line and converts the string to char_array and sortes the string and writes the string into the file.
using System;
using System.IO;
namespace FileIOApplication
{
class Program
{
static void Main(string[] args)
{
int counter = 0;
string line;
try{
System.IO.StreamReader file = new System.IO.StreamReader("I: est.txt");//reading file string line by line.
while((line = file.ReadLine()) != null){
char[] c=line.ToCharArray();
Array.Sort(c); //sorting into the alphabetize order
string sortedLine=String.Empty;
foreach (char ch in c)
sortedLine+=ch.ToString() ;
System.IO.File.WriteAllText ("c: est.txt", sortedLine); //it will write the sorted string in the same file.
}
file.Close();
Console.ReadLine();
}
catch (Exception e){Console.Write("Error while reading file");}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.