Please Help Me Get This Right... (C# Question)

koeks525

Executive Member
Joined
Jul 14, 2012
Messages
6,013
Reaction score
1,199
Location
Canada
Hi guys,

I have this C# Question:

1) I have to prompt the user for the number of idioms and write them to a file called "idioms.txt"

I can do this part no problem.

2) Read the lines from "idioms.txt" file and display the number of vowels of each idiom.

Looking at the screenshot of what this should like, I need to show the total of A, total of E, total of I, total of O, total of U.

CODE FOR THE PROMPTING AND WRITING TO FILE (1)

Code:
 int numberOfIdioms;
            string fileName = "idioms.txt";
            int countA = 0, countE = 0, countI = 0, countO = 0, countU = 0;
            
            Console.Title = "String Functions";

            Console.Write("Please enter number of idioms: ");
            numberOfIdioms = int.Parse(Console.ReadLine());

            string[] idioms = new string[numberOfIdioms];
            Console.WriteLine();

            for (int aa = 0; aa < idioms.Length; aa++)
            {
                Console.Write("Enter idiom {0}: ", aa + 1);
                idioms[aa] = Console.ReadLine();
            }

            StreamWriter myIdiomsFile = new StreamWriter(fileName);

            for (int a = 0; a < numberOfIdioms; a++)
            {
                myIdiomsFile.WriteLine("{0}", idioms[a]);
            }

            myIdiomsFile.Close();
            Console.Clear();

My problem is the second part of the question, I have done (so far) in the second part:

Code:
StreamReader readIdiomsFile = new StreamReader(fileName);

            while (readIdiomsFile.EndOfStream == false)
            {
                
                
                for (int b = 0; b < idioms.Length; b++)
                {
                    string line = readIdiomsFile.ReadLine().ToUpper();

                    if (line.Contains("A"))
                    {
                        countA++;
                    }

                    else if (line.Contains("E"))
                    {
                        countE++;
                    }
                    else if (line.Contains("I"))
                    {
                        countI++;

                    }
                    else if (line.Contains("O"))
                    {
                        countO++;
                    }
                    else if (line.Contains("U"))
                    {
                        countU++;
                    }

I have tried Googling this, and looking at Stackoverflow, but I only get counting all the vowels (not counting separate vowels).

Thanks
 
Okay so in part one you input, say "New York Minute", and in part 2 you want it to do this:

A 0
e 2
i 1
o 1
u 1

right?

You can use Regex for this:
Code:
countA = Regex.Matches(line ,"A").Count;
 
string line = readIdiomsFile.ReadLine().ToUpper();

Now take the line and split it into words.
Foreach word read each letter ,determine if vowel and append to master vowel counter(Acount,Ecount,Icount...)
 
Okay so in part one you input, say "New York Minute", and in part 2 you want it to do this:

A 0
e 2
i 1
o 1
u 1

right?

You can use Regex for this:
Code:
countA = Regex.Matches(line ,"A").Count;
Correct but he is learning so he should learn to do this the long hard way to improve his skills.
 
You might be able to change "readIdiomsFile.ReadLine().ToUpper();" to "readIdiomsFile.Read().ToUpper();"
It should work then.
 
var stringToCount = readIdiomsFile.Read().ToUpper();

foreach (var character in stringToCount) //Every string is an array
{
switch (character)
{
case "A" :
countA++;
break;
//Repeat this code for others : E,I,O,U
}
}
 
A couple of nitpicking points ... this is better in my opinion (rats, can't get the formatting right, sorry):


string line;
int b;
while (!readIdiomsFile.EndOfStream)
{
for (b = 0; b < idioms.Length; b++)
{
line = readIdiomsFile.ReadLine().ToUpper();
 
"int b;" is just wrong. It doesn't give you anything, besides irrelevant access to "b" while outside of the for-loop
 
We should be helping students by giving them direction, NOT answers.

All OP needed to do was think about how he would work it out normally as a human being (counting the chars in the each line) and then translate that to code. By giving him the regex function or coding the loop for him you basically did his homework for him.
 
We should be helping students by giving them direction, NOT answers.

All OP needed to do was think about how he would work it out normally as a human being (counting the chars in the each line) and then translate that to code. By giving him the regex function or coding the loop for him you basically did his homework for him.

But but but I want everyone to see how 1337 my programming skillz are.
 
Top
Sign up to the MyBroadband newsletter
X