Question

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

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

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

  • This program will require you to create a basic parser for an input string that is...

    This program will require you to create a basic parser for an input string that is somewhat like Python. Python has an older parser module and a newer ast module to manage pure Python syntax, but they won't be useful here. The program will read a data string, identify the control characters, and print out the overall structure of the string. Here's an example Input String: [{1}] Output: Number inside a dictionary inside a list Here are some basic rules...

  • I need help programming this program in C. 1.) Write a program that reads a message,...

    I need help programming this program in C. 1.) Write a program that reads a message, then checks whether it's a palindrome (the letters in the message are the same from left to right as from right to left), example is shown below: Enter a message: He lived as a devil, eh? Palindrome Enter a message: Madam, I am Adam. Not a Palindrome 2.) Ignore all characters that aren't letters. Use integer variables to keep track of positions in the...

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

  • Python program. Character-Analysis Program Problem Statement: Design a program - IN PYTHON - that asks the...

    Python program. Character-Analysis Program Problem Statement: Design a program - IN PYTHON - that asks the user to enter a string. The string will then be displayed back to the user. This is followed by determining the number of alphabetic characters, numeric characters, lower_case letters, upper_case letters, whitespace characters, and then displaying them. The user should be given additional chances to enter additional strings, and each time a string is entered, the above process is to be performed. The inputting...

  • Prompt the user to enter two character strings. The program will then create a new string...

    Prompt the user to enter two character strings. The program will then create a new string which has the string that appears second, alphabetically, appended to the end of the string that shows up first alphabetically. Use a dash (-') to separate the two strings. You must implement the function string appendStrings(string, string), where the return value is the combined string. Display the newly created string. Your program must be able to handle both uppercase and lowercase characters. You are...

  • Write a program(Python language for the following three questions), with comments, to do the following:   ...

    Write a program(Python language for the following three questions), with comments, to do the following:    Ask the user to enter an alphabetic string and assign the input to a variable str1. Print str1. Check if str1 is valid, i.e. consists of only alphabetic characters. If str1 is valid Print “<str1> is a valid string.” If the first character of str1 is uppercase, print the entire string in uppercase, with a suitable message. If the first character of str1 is...

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