Question

c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling...

c# please

Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling to check if the file exists before attempting to open it. Read and print each string to the console. Next modify each word such at the first letter in each word is uppercase and all other letters are lowercase. Display the modified word on the console.

Creating a text file:

Open up Notepad and type in the following words. Save the file as collectionOfWords.txt

computer, software, atlanta, programming, fun, lovely, morning, day, week, month, teacher, student, instructor, circle, walk, run, square, triangle, park, road, college, university, classroom, summer.

Sample Output:

software

Software

computer

Computer

Homework Answers

Answer #1

using System;

//System.IO contains File Class which we use to read from file

using System.IO;

class MainClass {

//main function

public static void Main (string[] args) {

//we try everything in try block if FileNotFoundException is raised it goes to catch block

try{

//fileContents has all the line from the file

var fileContents = File.ReadAllLines("wronginput.txt");

Console.WriteLine("Contents of the file:");

//foreach loop takes each element from the list`

//so here we iterate through each name from list and print to console

foreach(var name in fileContents){

Console.WriteLine(name);

}

//initialize two strings temporarily for below use

string temp,temp1;

//here we start modification of string

Console.WriteLine("\nContents of the file with modification:");

//iterate through each name from the list which contains contents of the file

foreach(var name in fileContents){

//store the first character of string which is converted to upper in temp variable

temp = name[0].ToString().ToUpper();

//take substring from index 1 to last i.e except first char take the remaining and convert it to lower

temp1 = name.Substring(1,name.Length-1).ToLower();

//print to the console by concatenating both the above strings

Console.WriteLine(temp+temp1);

}

}

//this catch block will hit only if FileNotFoundException occurs

catch (FileNotFoundException)

{

Console.WriteLine("File Not Found");

}

}

}

//output:

**************************************************************************
Please give an upvote,as it matters to me a lot :)
Got any doubts? Feel free to shoot them in the comment section
**************************************************************************

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT