#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 the number of Consonants in the string"<<endl;
cout << " (C)
Count both the vowels and consonants in the
string"<<endl;
cout << " (D)
Enter another string" << endl;
cout << " (E)
Exit program" << endl;
//Inputting choice
cout << "Enter choice:
";
cin.getline(choice, 2);
inputChoice = choice[0]; //a single
char variable will now equal the choice user pic from
c-string
switch (toupper(inputChoice)) //a
switch case, any user input char that is lower case will be now
uppercase
{
case
'A'://Function call to get number of Vowels
vowelTotal = numVowels(string);
cout << "Number of vowels is: " <<
vowelTotal << endl;
break;
case 'B':
//Function call to get number of consonants
consonantTotal = numConsonants(string);
//Outputting number of Consonants
cout << "Number of Consonants is: "
<< consonantTotal << endl;
break;
case 'C':
//Function call to get number of Vowels
vowelTotal = numVowels(string);
//Function call to get number of
consonants
consonantTotal = numConsonants(string);
//Outputting Both Vowels and Consonants
cout << "Number of vowels is: " <<
vowelTotal << endl;
cout << "Number of Consonants is:"
<< consonantTotal << endl;
break;
case 'D':
//Inputting another string
//Input a string
cout << "Enter a string: " <<
endl;
cin.getline(string, 100);
break;
case 'E':
exit(0);
break;
}//End of Switch
} while (inputChoice != 'E');//End Do while
}//End main
//Function Definitions
int numVowels(char *str)
{
int count = 0;//Local variable accumulates the total
number of vowels
while (*str != '\0')
{
if (*str == 'a' || *str == 'e' ||
*str == 'i' || *str == 'o' || *str == 'u')
count++;
str++;
}//End While
//Returns count value to function call
return count;
}
int numConsonants(char *str)
{
int count = 0;
//Checks for all characters in C-string
while (*str != '\0')
{ //Checks for consonants
if (*str != 'a' && *str !=
'e' && *str != 'i' && *str != 'o' && *str
!= 'u')
count++;
str++;
}//End While
//Returns count value to function call
return count;
}
the code works. the problem here is i dont want to have a set limit on the char array. (my cstring)..instead of just increasing the array size to 1000 and wasting space is there away where i can dynamically allocate an array with the char array. keep in mind "I DO NOT WANT TO JUST INCREASE THE ARRAY SIZE" thanks
Please follow the changes (highlighted in below code)made according to your use.
#include <iostream>
#include <cstring>
#include <string>
#include <istream>
#include <stdlib.h>
using namespace std;
//Function prototypes
int numVowels(char *str);
int numConsonants(char *str);
int main()
{
/*Modify your char string[100] to char *string*/
char *string;
/*dynamic allocation of memory to string array*/
string=(char *) malloc(sizeof(char));
char inputChoice, choice[2];
int vowelTotal, consonantTotal;
/*New variables used*/
int i=0;
char ch;
//Input a string
cout << "Enter a string: " << endl;
/*Instead of using getline, read the char by char till newline
char \n and store in array string*/
//cin.getline(string, 100);
while((ch=getchar())!='\n') {
realloc(string, (sizeof(char)));
string[i++]=ch;
}
/*Every string end with null char '\0'*/
string[i] = '\0';
do
{
//Displays the Menu
cout << " (A) Count the number of vowels in the
string"<<endl;
cout << " (B) Count the number of Consonants in the
string"<<endl;
cout << " (C) Count both the vowels and consonants in the
string"<<endl;
cout << " (D) Enter another string" << endl;
cout << " (E) Exit program" << endl;
//Inputting choice
cout << "Enter choice: ";
cin.getline(choice, 2);
inputChoice = choice[0]; //a single char variable will now equal
the choice user pic from c-string
switch (toupper(inputChoice)) //a switch case, any user input char
that is lower case will be now uppercase
{
case 'A'://Function call to get number of Vowels
vowelTotal = numVowels(string);
cout << "Number of vowels is: " << vowelTotal <<
endl;
break;
case 'B': //Function call to get number of consonants
consonantTotal = numConsonants(string);
//Outputting number of Consonants
cout << "Number of Consonants is: " << consonantTotal
<< endl;
break;
case 'C': //Function call to get number of Vowels
vowelTotal = numVowels(string);
//Function call to get number of consonants
consonantTotal = numConsonants(string);
//Outputting Both Vowels and Consonants
cout << "Number of vowels is: " << vowelTotal <<
endl;
cout << "Number of Consonants is:" << consonantTotal
<< endl;
break;
case 'D': //Inputting another string
i=0;/*this i=0 will deallocates the string, and
removes the array contents*/
cout << "Enter a string: " << endl;
while((ch=getchar())!='\n') {
realloc(string, (sizeof(char)));
string[i++]=ch;
}
string[i] = '\0';
break;
case 'E': exit(0);
break;
}//End of Switch
} while (inputChoice != 'E');//End Do while
}//End main
//Function Definitions
int numVowels(char *str)
{
int count = 0;//Local variable accumulates the total number of
vowels
while (*str != '\0')
{
if (*str == 'a' || *str == 'e' || *str == 'i' || *str == 'o' ||
*str == 'u')
count++;
str++;
}//End While
//Returns count value to function call
return count;
}
int numConsonants(char *str)
{
int count = 0;
//Checks for all characters in C-string
while (*str != '\0')
{ //Checks for consonants
if (*str != 'a' && *str != 'e' && *str != 'i'
&& *str != 'o' && *str != 'u')
count++;
str++;
}//End While
//Returns count value to function call
return count;
}
Output:
Enter a string:
abdcdef
(A) Count the number of vowels in the string
(B) Count the number of Consonants in the string
(C) Count both the vowels and consonants in the string
(D) Enter another string
(E) Exit program
Enter choice: A
Number of vowels is: 2
(A) Count the number of vowels in the string
(B) Count the number of Consonants in the string
(C) Count both the vowels and consonants in the string
(D) Enter another string
(E) Exit program
Enter choice: B
Number of Consonants is: 5
(A) Count the number of vowels in the string
(B) Count the number of Consonants in the string
(C) Count both the vowels and consonants in the string
(D) Enter another string
(E) Exit program
Enter choice: C
Number of vowels is: 2
Number of Consonants is:5
(A) Count the number of vowels in the string
(B) Count the number of Consonants in the string
(C) Count both the vowels and consonants in the string
(D) Enter another string
(E) Exit program
Enter choice: D
Enter a string:
appleu
(A) Count the number of vowels in the string
(B) Count the number of Consonants in the string
(C) Count both the vowels and consonants in the string
(D) Enter another string
(E) Exit program
Enter choice: A
Number of vowels is: 3
(A) Count the number of vowels in the string
(B) Count the number of Consonants in the string
(C) Count both the vowels and consonants in the string
(D) Enter another string
(E) Exit program
Enter choice: C
Number of vowels is: 3
Number of Consonants is:3
(A) Count the number of vowels in the string
(B) Count the number of Consonants in the string
(C) Count both the vowels and consonants in the string
(D) Enter another string
(E) Exit program
Enter choice: E
Code pics:
Please rate the solution, your rating is precious :)
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...
#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...
I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int main() { string s; cout<< "Enter a string" <<endl; getline (cin,s); cout<< s <<endl; int vowels=0,consonants=0,digits=0,specialChar=0; for (int i=0; i<s.length(); i++) { char ch=s[i]; if (isalpha(s[i])!= 0){ s[i]= toupper(s[i]); if (ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o' || ch == 'u') vowels++; else consonants++; } else if (isdigit(s[i])!= 0) digits++; else specialChar++; } cout<<"Vowels="<<vowels<<endl; cout<<"Consonants="<<consonants<<endl; cout<<"Digits="<<digits<<endl; cout<<"Special Characters="<<specialChar<<endl;...
#include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...
CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { const int index = 5; int head = 0; string s[index]; int flag = 1; int choice; while (flag) { cout << "\n1. Add an Item in the Chores List."; cout << "\n2. How many Chores are in the list."; cout << "\n3. Show the list of Chores."; cout << "\n4. Delete an...
moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...
#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:...
#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() { char board[SIZE]; int player, choice, win, count; char mark; count = 0; // number of boxes marked till now initBoard(board); // start the game player = 1; // default player mark = 'X'; // default mark do { displayBoard(board); cout << "Player " << player << "(" << mark...