Question

In C Language Write a program that reads characters from scanf1 (keyboard) and gives the number...

In C Language

Write a program that reads characters from scanf1 (keyboard) and gives the number of vowels (lower case) entered, stopping when ‘q’ has been entered. Construct three versions of this program, using the while, do-while, and for loops.

3 different version


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Version 1 code:

#include<stdio.h>

void main(){
    char chr = 'm'; // variable to store the character, here we can use cahr value to any thing other than q 
    int   count;  // variable to store number of vowels
    while (chr != 'q'){ // while loop count vowels
        printf("Enter a character(q to quit): \n"); //  message
        scanf(" %c", &chr); // usr input
        if( chr == 'a'|| chr == 'e'|| chr == 'i'|| chr == 'o'|| chr == 'u'){ // check if charcter is vowels
            count++; // update count variable
        }
    }
    printf("Total number of vowels are: %d\n", count); // print the vowels

}

Sample Screenshot:

root@kali:-/code# gcc version1.c -o version1 root@kali:-/code# -/version1 Enter a character(q to quit): а Enter a character(q

Version2 :

#include<stdio.h>

void main(){
    char chr = 'm'; // variable to store the character, here we can use cahr value to any thing other than q 
    int   count;  // variable to store number of vowels
  for(;;) {   // for loop count vowels
        printf("Enter a character(q to quit): \n"); //  message
        scanf(" %c", &chr); // usr input
        if( chr == 'a'|| chr == 'e'|| chr == 'i'|| chr == 'o'|| chr == 'u'){ // check if charcter is vowels
            count++; // update count variable
        }
    if (chr == 'q'){
        printf("Total number of vowels are: %d\n", count); // print the vowels
        return;
    }
    }
}

sample screenshot:

root@kali:-/code# gcc version2.c -o version2 root@kali:-/code# -/version2 Enter a character(q to quit): W Enter a character(q

Version 3:

#include<stdio.h>

void main(){
    char chr = 'm'; // variable to store the character, here we can use cahr value to any thing other than q 
    int   count;  // variable to store number of vowels
  do {   // do loop count vowels
        printf("Enter a character(q to quit): \n"); //  message
        scanf(" %c", &chr); // usr input
        if( chr == 'a'|| chr == 'e'|| chr == 'i'|| chr == 'o'|| chr == 'u'){ // check if charcter is vowels
            count++; // update count variable
        }
    }while (chr != 'q');
    printf("Total number of vowels are: %d\n", count); // print the vowels

}

Sample output:

root@kali:-/code# gcc version1.c -o version1 root@kali:-/code# -/version1 Enter a character(q to quit): a Enter a character(q

Note: Please let me know if you have any doubt.

Add a comment
Know the answer?
Add Answer to:
In C Language Write a program that reads characters from scanf1 (keyboard) and gives the number...
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
  • 3. Write a program in assembly language that reads a number from the keyboard and displays...

    3. Write a program in assembly language that reads a number from the keyboard and displays it on the screen. 4. Write a program in assembly language to ask two digits from the user, store the digits in the EAX and EBX register, respectively, add the values, store the result in a memory location 'res' and finally display the result.

  • Part A Write a Java program that reads an integer n from the keyboard and loops...

    Part A Write a Java program that reads an integer n from the keyboard and loops until −13 ≤ n ≤ 13 is successfully entered. A do-while loop is advised. Part B Write a Java program that reads an integer n from the keyboard and prints the corresponding value n!. [This is n factorial]. You must verify that the input integer satisfies the constraint 0 ≤ n ≤ 13; keep looping until the constraint is satisfied.   Will give thumbs up...

  • C language 2. Write a program to read in a string from the keyboard and only...

    C language 2. Write a program to read in a string from the keyboard and only display the vowels and their positions index) in a table format. Enter a string: This is a test Here are the vowels and their positions:

  • in c++ language, using quincy compiler Write a program that will read in 12 characters from...

    in c++ language, using quincy compiler Write a program that will read in 12 characters from the keyboard between a – z in lowercase. These letters will be inserted into a queue (imported from the Queue.h header file). When the 12th character has been entered, the queue will serve the characters one at a time to a character variable, which will push the values, one at a time, into a stack (using code imported from the Stack.h header file) and...

  • Write a C program that acquires a sequence of characters from the keyboard. The sequence ends...

    Write a C program that acquires a sequence of characters from the keyboard. The sequence ends once the user enters the newline character, i.e., hits the ENTER key. The program modifies the entered sequence by: • First replacing every occurrence of the sequence "ch” with the letter "K" • Then replacing all occurrences of the sequence "oo" with the letter "u" . And then replacing all occurrences of the sequence "ee" with the letter "j" and prints the modified version...

  • c++ language Write a program that contains the following functions, i. A function isVowel that takes...

    c++ language Write a program that contains the following functions, i. A function isVowel that takes in a character and returns true if it is a vowel and false otherwise ii. A function that request from the user to enter in a sequence of characters, and outputs the number of vowels entered. (Use Function in a) to assist you, a sentinel value can be used to specify the end of the user input iii. A function that reads 3 test...

  • Write a C program that reads characters from a text file, and recognizes the identifiers in...

    Write a C program that reads characters from a text file, and recognizes the identifiers in the text file. It ignores the rest of the characters. An identifier is a sequence of letters, digits, and underscore characters, where the first character is always a letter or an underscore character. Lower and upper case characters can be part of the identifier. The recognized identifier are copied into the output text. b. Change the C program in exercise 2, so that all...

  • Write a program to draw this number triangl using MIPS assembly language The number of rows is inputted from keyboard....

    Write a program to draw this number triangl using MIPS assembly language The number of rows is inputted from keyboard. 6. Write a program to draw this number triangle 1 1〈--|--〉1 The number of rows is inputted from keyboard. 6. Write a program to draw this number triangle 1 1〈--|--〉1 The number of rows is inputted from keyboard.

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. The program must be in one file. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to...

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