Question

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 also output the index in the second array where each vowel first appeared or a suitable message if a particular vowel is not in the array at all.
(Programming exercise)
Implement the program from Question 1 into a C program. The program should take suitable steps to trap and recover from errors.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ALGORITHM:

input: string
if c:string is vowel
v <- c
for each c in v
count <- 0   
count = unique c:v
if count==1
first_time <- v:c
counts <- count
output: v,counts,first_time

C-PROGRAM:

#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;
}

Enter a string of characters(lower case): qaswedfrtghyujiklmnbvcxzasderb Vowels present: a eu i a e Vowels count: a: 2 e: 2 u

COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~

Add a comment
Know the answer?
Add Answer to:
Create an algorithm for a program that reads a string with a maximum length of 30 from the keyboard. The algorithm sho...
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...

  • 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...

  • Please do it with C++. Thanks:) Write a program that reads a string and a character...

    Please do it with C++. Thanks:) Write a program that reads a string and a character from the keyboard. The program should output how many times the character appears in the string. Make the program run in a loop until the string finish is input. Sample run: Input a string: alicia Input a letter: a The letter appears 2 times Input a string: felix Input a letter: x The letter appears 1 time Input a string: finish Good Bye.

  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • Write a C++ program that reads in 6 elements from the keyboard and stores them in...

    Write a C++ program that reads in 6 elements from the keyboard and stores them in an array x[6]. It should then create a second array y[6] that contains the elements from x in reverse order and with the s Tsecond - Tsmallest shown below. A sample run should look like: rocted ITrom it (e.g., Jlast first Tsmallest Vsecond last , etc). The program should finally print out both arrays to screen, in the format Enter 6 array elements: 4...

  • Q1. CLO: (5 points) Write a java program that reads 10 words from the keyboard then...

    Q1. CLO: (5 points) Write a java program that reads 10 words from the keyboard then place them in an array. Display the array elements in reverse order, and then count and print the number of times a word appears in that array. The user shall insert the word from the keyboard.

  • ((python programming)) 2. In this exercise you will create a program that reads a letter of...

    ((python programming)) 2. In this exercise you will create a program that reads a letter of the alphabet from the user. If the user enters a, e, i, o or u then your program should display a message indicating that the entered letter is a vowel. If the user enters y then your program should display a message indicating that sometimes y is a vowel and sometimes y is a consonant. Otherwise your program should display a message indicating that...

  • 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.

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

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