Question

c programming

Write a c program that reads a string with a maximum length of 30 from the keyboard[1]. The program should then copy the characters from that input string into a second character array (in order of input). However, only if a character is a vowel (a, e, i, o, u) should it be copied into the second array. Once copied, the program should output the values stored in the second array (in order of input). The program should then count and display the number of times each vowel appears in the second array. Also, the program should determine and output the index in the second array where each vowel first appeared or display a suitable message if a particular vowel is not in the array at all.

 

Your solution must be modular in design; you should consider in your design the issues of high cohesion and code re-use (refer to the lecture notes for module four, on modular design and programming).

 

 



[1]     Remember strings differ from a regular character array by the inclusion of the null character (‘\0’) following the last alphabetical character. A regular character array just stored characters and does not include null.


0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<stdio.h>
int main(){
int C = 30; //maximum string length
char sentence[C];
char v[C];
char vowels[] = {'a','e','i','o','u'};
// INPUT THE STRING, STORED IN sentence[]
printf("\nEnter a string of characters(lower case): ");
for(int i=0;i<C;i++){
scanf("%c",&sentence[i]);
}
//CHECKING FOR VOWELS
int j = 0;
for(int i=0;i<C;i++){
if(sentence[i]=='a'||sentence[i]=='e'||sentence[i]=='i'||sentence[i]=='o'||sentence[i]=='u'){
v[j] = sentence[i];
j = j+1;
}
}
//DISPLAY THE VOWELS PRESENT IN THE INPUT STRING
int N = j; //number of vowels present in the string
printf("\nVowels present: ");
if(N>0){
for(int i=0;i<N;i++){
printf("%c ",v[i]);
}
}
// COMPUTE THE COUNT FOR EACH VOWEL AND THE INDEX WHERE THEY APPEARED FIRST
int n,vcount[] = {0,0,0,0,0}; // count 0 means vowel never appeared
int first_appeared[] = {-1,-1,-1,-1,-1}; // index -1 means vowel never appeared
if(N>0){
for(int i=0;i<5;i++){ // 5 for the number of vowels known
n = 0;
for(int k=0;k<N;k++){
if(v[k]==vowels[i]){
if(n==0){
first_appeared[i] = k;
}
n = n+1;
}
}
vcount[i] = n;
}
}
// DISPLAYING THE COUNT FOR EACH VOWEL
printf("\nVowels count: \n");
for(int i=0;i<5;i++){ //5 for the number of vowels known
if(vcount[i]>0)
printf("%c: %d\n",vowels[i],vcount[i]);
}
//DISPLAYING THE INDEX OF FIRST APPEARANCE
printf("\nIndex of first appearance: \n");
for(int i=0;i<5;i++){ //5 for the number of vowels known
if(first_appeared[i]>=0)
printf("%c: %d\n",vowels[i],first_appeared[i]);
else
printf("%c: never appeared\n",vowels[i]);
}
return 0;
}


answered by: Martin
Add a comment
Know the answer?
Add Answer to:
c programming
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create an algorithm for a program that reads a string with a maximum length of 30...

    Create an algorithm for a program that reads a string with a maximum length of 30 from the keyboard. The algorithm should then copy the characters from that string into a second character array in order but only if the character is a vowel (a, e, i, o, u). Once copied, the algorithm should output these values in order. The algorithm should then count and display the number of times each vowel appears in the array. Finally, the algorithm should...

  • Create an algorithm for a program that reads a string with a maximum length of 30 from the keyboard. The algorithm sho...

    Create an algorithm for a program that reads a string with a maximum length of 30 from the keyboard. The algorithm should then copy the characters from that string into a second character array in order but only if the character is a vowel (a, e, i, o, u). Once copied, the algorithm should output these values in order. The algorithm should then count and display the number of times each vowel appears in the array. Finally, the algorithm should...

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • USING RAPTOR For the following Programming Challenges, use the modular approach and pseudocode to design a suitable program to solve it. Create a program that allows the user to input a list of first...

    USING RAPTOR For the following Programming Challenges, use the modular approach and pseudocode to design a suitable program to solve it. Create a program that allows the user to input a list of first names into one array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. The output should be a list of email addresses where the address is of the following from: [email protected]

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

  • Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program...

    Write in C. Simple Program (beginner) Assignment: Write a program Character Pointers and Functions. (like program #5-5). Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count. <END>

  • Can someone help me with this, and it has to be written in the C programming...

    Can someone help me with this, and it has to be written in the C programming language: Write a program that reads a string from the keyboard. If the length of the string is an even number, your program should split the string into two strings of equal length. If the length of the string is odd, your program should split the string into two strings where the first part has one more character than the second part. Your program...

  • C programming SPTL You have been hired by a company that makes keyboards. The company is...

    C programming SPTL You have been hired by a company that makes keyboards. The company is doing a test to see which keys on the keyboard are used the most and the least. The assumption is the keys used the most will have to have better hardware and the keys used the least can have cheaper hardware. In order to be competitive in the marketplace, it is important to obtain data on the use of the keys on the keyboard...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT