// Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less.
// The program should prompt the user for a string.
// The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string.
// Vowels: a, e, i, o, u.
// Output the entered string, how many of each type of vowel are in the string and the total number of vowels in the string.
// Run 1: “Are Zoo’s a fun place to visit?”
// Run 2: “#1 Diablo Valley College”
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;
int numChars(char *, char);
int main() {
const int SIZE = 51;
char inputStr[SIZE];
char vowels[]= {'a', 'e', 'i', 'o', 'u'};
cout << "Enter a string (up to 50 characters): " << endl;
cin.getline(inputStr, SIZE);
cout << vowels << " appears " << endl;
cout << numChars(inputStr, vowels) << " times.\n"
return 0;
}
int numChars(char *ptr, char ch) {
int vowelCount = 0;
while(*ptr != '\0') {
if(*ptr == ch)
vowelCount++;
ptr++;
}
return vowelCount;
}
//This is the code I've written so far. Can anyone help me figure it out? Am I on the right track or am i I doing it correctly? Is there a less complicated way of doing it? Thank you
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;
// Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less.
// The program should prompt the user for a string.
// The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string.
// Vowels: a, e, i, o, u.
// Output the entered string, how many of each type of vowel are in the string and the total number of vowels in the string.
// Run 1: “Are Zoo’s a fun place to visit?”
// Run 2: “#1 Diablo Valley College”
int numChars(char *, char);
int main() {
const int SIZE = 51;
char inputStr[SIZE];
char vowels[]= {'a', 'e', 'i', 'o', 'u'};
cout << "Enter a string (up to 50 characters): " << endl;
cin.getline(inputStr, SIZE);
cout << vowels << " appears " << endl;
cout << numChars(inputStr, vowels) << " times.\n"
return 0;
}
int numChars(char *ptr, char ch) {
int vowelCount = 0;
while(*ptr != '\0') {
if(*ptr == ch)
vowelCount++;
ptr++;
}
return vowelCount;
}
Program:
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;
// Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less.
// The program should prompt the user for a string.
// The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string.
// Vowels: a, e, i, o, u.
// Output the entered string, how many of each type of vowel are in the string and the total number of vowels in the string.
// Run 1: “Are Zoo’s a fun place to visit?”
// Run 2: “#1 Diablo Valley College”
int numChars(char *, char *, int *);
int main() {
const int SIZE = 51;
char inputStr[SIZE];
char vowels[]= {'a', 'e', 'i', 'o', 'u'};
//array to hold the counts of each vowels, initialize all counts to 0
int count[] = {0, 0, 0, 0, 0};
int numberOfVowels;
//prompt and read the string
cout << "Enter a string (up to 50 characters): " << endl;
cin.getline(inputStr, SIZE);
//get the number of vowels using the numChars function
numberOfVowels = numChars(inputStr, vowels, count);
//output the string entered:
cout << endl << "String entered: " << inputStr << endl;
//output how many of each type of vowel are in the string
cout << endl << "Count of each type of vowel appeared in the string: " << endl;
for(int i = 0; i < 5; i++)
{
cout << vowels[i] << " - " << count[i] << endl;
}
//output total number of vowels in the string
cout << endl << "Total number of vowels in the string: " << numberOfVowels;
return 0;
}
//function that counts how many times each vowel appeared and returns the total number of vowels in the string
int numChars(char *ptr, char *ch, int *count)
{
int vowelCount = 0;
//run a loop that get each character in the string
while(*ptr != '\0')
{
//for each character, check if it is vowel or not
for(int i = 0; *(ch+i) != '\0'; i++)
{
//if vowel increment the count in the count array and also increment the total count
if(tolower(*ptr) == *(ch + i))
{
*(count + i) = *(count + i) + 1;
vowelCount++;
}
}
ptr++;
}
//return the total number of vowels
return vowelCount;
}
Output:
Let me know if you have any concerns with the above solution.
// Write a program that determines how many of each type of vowel are in an...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...
Take the following C++ code and add to it the following: Write a program that reads a string and outputs the number of times each lowercase vowel appears in it. Your program must contain a function with one of its parameters as a char variable, and if the character is a vowel, it increments that vowel's count. #include<iostream> #include<string> using namespace std; int countVowel(char, char); int main(void) { string str1; int countA = 0; int countE = 0; int countI...
CHALLENGE ACTIVITY 8.4.2: Find char in C string Assign a pointer to any instance of searchChar in personName to searchResult. #include <iostream> #include <cstring> using namespace std; int main() { char personName[100]; char searchChar; char* searchResult = nullptr; cin.getline(personName, 100); cin >> searchChar; /* Your solution goes here */ if (searchResult != nullptr) { cout << "Character found." << endl; } else { cout << "Character not found." << endl; } return 0; }
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...
This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...
I'm having trouble getting my program to output this statement Enter a sentence: Test! There are 5 total characters. There are 1 vowel. There are 1 UPPERCASE letters. There are 3 lowercase letters. There are 1 other characters. Here's my code: #include<string.h> #include<stdio.h> #include<stdbool.h> int main() { char str[100]; int i; int vowels=0; int UC; int LC; int Others; int c; printf("Enter a sentence: \n"); gets(s); LC=countLC(&s); UC=countUC(&s); Others=countOthers(&s); printf("There are %d total characters.\n", ; for(i=0; i<strlen(str); i++){ if(isVowel(str[i])) vowels++;...
I was asked to write a program that when divisible by 2- reverses the order of the letters in a sentence when divisible by 3- changes all lowercase letter into uppercase letters in a sentence when divisible by 5 skips the vowels in a sentence this is what I have so far. my reverseMode works fine. #include <iostream> #include <string> #include<cstring> using namespace std; void reverseMode(char* str); void upperMode(char* str); void goodbyeVowels(char* str); char str[50], rstr[50]; //initializing my character arrray...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...