1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
pantera1 [17]
3 years ago
13

Write a C program that counts the number of vowels in a word. The C program reads words from an input file and then stores in an

output file the word and its number of vowels. At the end it also stores the word with the most and the word with the fewest vowels. Extra bonus: Your program should handle the situation in which there are several words with the maximum and minimum number of vowels.
Computers and Technology
1 answer:
polet [3.4K]3 years ago
5 0

Answer:

Explanation:

The following code is written in C and does what the question requires. It uses the input file reads it and outputs the number of vowels to the file called output.dat

#include <ctype.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int str_count_in_chars(char *start, char *end, char *chars) {

   int count = 0;

   for (; start != end; count += !!strchr(chars, *(start++)));

   return count;

}

void str_swap_in_chars(size_t str_len, char **str, char *chars) {

   for (int front = 0, back = str_len - 1; front < back; front++) {

       if (strchr(chars, (*str)[front])) {

           for (; !strchr(chars, (*str)[back]); back--);

           char tmp = (*str)[front];

           (*str)[front] = (*str)[back];

           (*str)[back--] = tmp;

       }

   }

}

char *file_to_str(FILE *fin) {

   int buf_len = 64;

   char buf[buf_len];

   char *str = malloc(buf_len);

   str[0] = '\0';

   for (int i = 1; fgets(buf, buf_len, fin); i++) {

       if (!(str = realloc(str, i * buf_len))) {

           fprintf(stderr, "%s:%d realloc failed\n", __FILE__, __LINE__);

           exit(1);

       }

       strcat(str, buf);

   }

   return str;

}

int main() {

   char *vowels = "aeiou";

   FILE *fin = fopen("input.dat", "r");

   FILE *fout = fopen("output.dat", "w");

   if (!fin || !fout) {

       fprintf(stderr, "%s:%d fopen failed\n", __FILE__, __LINE__);

       exit(1);

   }

   char *words = file_to_str(fin);

   fclose(fin);

   int words_len = strlen(words);

   for (int i = 0; i < words_len;) {

       if (isspace(words[i])) {

           fputc(words[i++], fout);

           continue;

       }

       int start = i;

       for (; i < words_len && !isspace(words[i]); i++);

       char *word = words + start;

       int word_len = i - start;

       int vowel_count = str_count_in_chars(word, words + i, vowels);

       if (vowel_count % 2 == 0) {

           str_swap_in_chars(word_len, &word, vowels);

       }

       fprintf(fout, "%.*s_%dvow", word_len, word, vowel_count);

   }

   fclose(fout);  

   free(words);

   return 0;

}

You might be interested in
A _____________ is designed for a individual user.
pishuonlain [190]

Answer:

i don't know it sorry

Explanation:

7 0
3 years ago
Why is it important to carefully order events in a personal narrative?
Anna11 [10]

Why is it important to carefully order events in a personal narrative? because it strengthens the effect the beginning will have on readers, enabling them to relate to the experience because sequencing events in a way that makes sense will keep readers engaged and enable the story to have a strong impact because it will help the ending make more sense if the beginning and the middle are well written with many sensory details because the beginning sets the stage for everything that will be shown first

Hope this helps :)

3 0
4 years ago
What is smallpdf(app) for? How can I use it? ​
Fynjy0 [20]
Smallpdf app is. For windows
Go to your homepage and click a tool
Drag a file into the colored toolbox
Most tools should start working on the file right away
For some tools, further optimization options may be available
Download the converted doc to your local drive
O
)
O
8 0
3 years ago
What is your personal definition of life? How do you appreciate life?​
kompoz [17]
The definition of life is to fulfill your purpose.To accomplish your goals and enjoy life.Appreciate the little things that happen in your life and look out for others and love unconditionally.Have a positive attitude in life.Love Yourself!
3 0
3 years ago
What permissions are needed in order to use a work online that is in the public domain?
Maurinko [17]
<h2 /><h2>⇒Written  \: permission  \: from \\  the  \: creator</h2>

The term “public domain” refers to creative materials that are not protected by intellectual property laws such as copyright, trademark, or patent laws. ... Anyone can use a public domain work without obtaining permission, but no one can ever own it.

5 0
3 years ago
Other questions:
  • In Microsoft word,when you highlight existing text you want to replace ,you’re in?
    12·1 answer
  • I have one big question <br><br><br> WHAT CAN I DO WHEN I'M BORED
    6·2 answers
  • Stacey wants to change her document to landscape view instead of portrait. Jesse wants to change the scale of his document. Whic
    5·1 answer
  • Which of the following companies develop, own, and provide travel products for people?
    8·1 answer
  • Complete the PizzaCaloriesPerSlice() function to compute the calories for a single slice of pizza. A PizzaCalories() function re
    6·1 answer
  • -How long does a copyright last?
    6·1 answer
  • Suppose the daytime processing load consists of 65% CPU activity and 35% disk activity. Your customers are complaining that the
    15·1 answer
  • Which vulnerability can occur if a programmer does not properly validate user input and allows an attacker to include unintended
    13·1 answer
  • ) Write a program to calculate the time to run 5 miles, 10 miles, half marathon, and full marathon if you can run at a constant
    12·1 answer
  • What is shotgun microphone?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!