Question

Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing...

Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing two functions that will output the number of vowels and consonants in a user inputted string. We’ll be using functions from the cstring library, so be sure to include it! We’ll be calling those functions method_one and method_two. To start off, declare a character array (with no size) called vowels and initialize it with “aeiouyAEIOUY” in our main function. Declare a character array with size 100 and call it u_string. Using the getline() function from iostream, you will use it to get a string from the user. So in main we should have the following statements:
char vowels[] = “ aeiouyAEIOUY”; char u_string[SIZE]; // SIZE is a global constant integer initialized with a value of 100
cout << “Please enter a string to determine the number of vowels and consonants it has:\n”; cin.getline(u_string, SIZE); method_one(vowels, u_string); method_two(vowels, u_string);
Our two functions will take in vowels and u_string so they will have function prototypes like so:
void method_one(char vow[], char usr_str[]); void method_two(char vow[], char usr_str[]);
The first method is using the strchr function from the cstring library. This function takes in the string to be searched (our c-string containing our vowels) as the first argument and our character as the second argument (from the user inputted string). The function returns a pointer to the first occurrence of that character in vowels character array. If it doesn’t find that character in our vowels c-string, then it returns a null pointer (nullptr). We need to check if the value returned is not a null pointer, if it’s not then we increment our counter that holds the number of vowels of our user inputted string has. If it is, then we increment our counter that holds the number of consonants of our user inputted string. So you’ll have to iterate through every single character in our usr_str character array and check them individually if they a letter first (using the isalpha function from cstring). If they are a letter, then we check if it’s a vowel or a consonant

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

C++ PROGRAM:

#include <cstring>
#include <iostream>
using namespace std;
const int SIZE = 100;
void method_one(char vow[], char usr_str[]);
void method_two(char vow[], char usr_str[]);
int main() {
    char vowels[] = "aeiouyAEIOUY";
    char u_string[SIZE];  // SIZE is a global constant integer initialized with a value of 100
    cout << "Please enter a string to determine the number of vowels and consonants it has :\n";
    cin.getline(u_string, SIZE);
    method_one(vowels, u_string);
    method_two(vowels, u_string);
    return 0;
}
void method_one(char vow[], char usr_str[]) {
    int vowels = 0;
    for (int i = 0; i < strlen(usr_str); i++) {
        if (isalpha(usr_str[i]) && strchr(vow, usr_str[i]) != NULL)
            vowels++;
    }
    cout << "Number of vowels in the user string: " << vowels << endl;
}
void method_two(char vow[], char usr_str[]) {
    int consonants = 0;
    for (int i = 0; i < strlen(usr_str); i++) {
        if (isalpha(usr_str[i]) && strchr(vow, usr_str[i]) == NULL)
            consonants++;
    }
    cout << "Number of consonants in the user string: " << consonants << endl;
}

SAMPLE OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing...
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
  • Help please this is C/C++ code /* * Lab12, the purpose of this lab is to...

    Help please this is C/C++ code /* * Lab12, the purpose of this lab is to improve your skills in handling c strings * with pointers . * use of : strlen ,string concatenation strcat, compare strcmp, * copy strcpy and search strrchr * */ #include <cstdlib> #include <iostream> #include<cstring> using namespace std; /** * Create a method that prompts the user for only lowercase letters to represent * a name. * Start a Label, then prompt the user to...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

  • in C++ Description The purpose of this challenge is to manually manipulate character arrays. This challenge...

    in C++ Description The purpose of this challenge is to manually manipulate character arrays. This challenge converts text in phone numbers to their equivalent values on a phone keypad. Requirements Write a program to decode phone numbers with letters in them. Create a void function decodetext(char after[], char before[]). This function should be case-insensitive. Do NOT cout in this function. Declare a char array to hold a maximum of 50 characters. Ask the user to enter a string. Use cin.getline(char_variable,...

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...

  • 2) Write a function named VowelConsonant which will have three parameters - all "C type" strings....

    2) Write a function named VowelConsonant which will have three parameters - all "C type" strings. The function will place all vowels (the characters a, e, i, o, u) from the first string into the second string and all consonants from the first string into the third string. Assume the size of each string is sufficient hold the characters. Case of letters is not significant. (15 points)

  • Malloc function For the prelab assignment and the lab next week use malloc function to allocate...

    Malloc function For the prelab assignment and the lab next week use malloc function to allocate space (to store the string) instead of creating fixed size character array. malloc function allows user to allocate memory (instead of compiler doing it by default) and this gives more control to the user and efficient allocation of the memory space. Example int *ptr ptr=malloc(sizeof(int)*10); In the example above integer pointer ptr is allocated a space of 10 blocks this is same as creating...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like...

    Need help writing this lab. Directions in lab attachment above. Thank you. Output should look like ones in attachment ab Ass Part 1 sing Static Arra Do this part on your own. Write a program named lab11 a.c containing the following function: preconditions arc is terminated by dest is big enough hold arc postconditions dest contains src and is terminated by "10" void my strcpy (char dest const char srclj) This function will take two character arrays as parameters. The...

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