Problem 20 vowel count

Back to Problem Solutions forum

sleepijs     2015-12-22 20:27:27
User avatar

I'm new to programming, I've read a few books about C and watched a few tutorials. Now I thought it would be a good idea to start writing stuff. I've solved a few problems, but I'm getting stuck on this one. :( http://www.codeabbey.com/index/task_view/vowel-count I'm stuck at the beggining. How do I add multiple lines of test cases?

This used to work before with integers and floats:

printf("Enter the values: \n");
for (i=0; i<n; ++i){
    scanf(" %d", &input[i]);
}

But it doesn't anymore.. I know I need to change a few things there. I thought something like this would work:

printf("Enter lines: \n");
for (i=0; i<n; i++){
    gets(lines[i]);
    }

I'm sure it's something simple, but I can't seem to find whats the problem.

The furtherest I've got is:

puts("Enter lines:");
for (i=0; i<n; ++i){
gets(input);
strcat(lines, input);

Which joins two strings..

Quandray     2015-12-23 08:05:13
User avatar

Hi, yes it's a good idea to start writing code!

For this problem, you need to read a number on the first line, then read that many lines of text. For each line of text you need to count the vowels.

To read the data you could do something like this

int numlines;
char aline[256];

scanf("%d",&numlines);  /* reads the number */
gets(aline);            /* reads the end of the line after the number */
while(numlines--){
  gets(aline);          /* reads a line of text */

  /* count the vowels in aline here */

}

Note that I've not included any printf prompts. That's because you're going to be running this code many times while you are writing it and it would be very tedious to type in the number and lines of text each time. I suggest you find out how to redirect stdin so it reads from a file. How you do that may depend on the compiler you are using. In the compiler I use, there's a config option under debugging to set the command argument <input.txt to read from a file called input.txt. Another way that might work for you is the C command

freopen("input.txt","r",stdin);

Start off with input.txt containing the example input data, so you know the answers your code should produce.

sleepijs     2015-12-23 20:49:17
User avatar

Thanks! This helped me a lot, at some points I was about to rage quit lol. :) I know it's a begginers problem and it shouldn't take me so much time to complete it, but I'm still a begginer. Hope over time I get better at writing code. :)

Quandray     2015-12-24 16:02:35
User avatar

Well done for solving it and well done for reading the input data from a file.

Now that you've solved it, have a look at the way other people solved it in C/C++.

Your code reads the input data twice.

Before you move on to the next problem, I suggest you change your input file so that the first line contains the number of data lines that follow. Then try to solve the problem by only reading the input data once.

You may want to use the fscanf function to read the number in the first line.

sleepijs     2015-12-25 17:14:35
User avatar

Did it. Thanks again for help! :)

Quandray     2015-12-25 19:56:02
User avatar

That's good. Now can you please try the following changes

Replace

fp = fopen("vowelCount.txt", "r");

with

freopen("vowelCount.txt","r",stdin);

Replace

fscanf(fp, " %d\n", &numberOfLines);

with

scanf("%d\n", &numberOfLines);

Replace

fgets(text, 300, fp);

with

gets(text);

Remove the fclose

Note that the \n in the fscanf/scanf statement is working ok for this problem, but it isn't doing what you think it is doing. It's better to do

scanf("%d", &numberOfLines);
gets(text);     /* this reads the remainder of the first input line */
sleepijs     2015-12-26 15:23:34
User avatar

Wasn't expecting this kind of help when I posted on the forums. :) freopen lets me open a file as standard input? Is it better to use it instead of fopen ?

Quandray     2015-12-26 19:39:25
User avatar

With the problems on this site, the intention is that you read from standard input and write to standard output. The closer your code is to doing that, the easier it is for other people to try to help help you.

If it reads from standard input, people can easily try your code from the problem page by pressing the C++ button.

I learnt C after being able to code in BASIC, Z80 assembler and maybe a few other languages. I can't imagine C is easy to learn as a first language, so I'm happy to help.

sleepijs     2015-12-28 09:21:34
User avatar

I'll keep that in mind. This is not my first attempt at learning C. But this time I'm sticking to it, and getting some results. :)

Please login and solve 5 problems to be able to post at forum