When user input number from 1 - 26 (which mean a to z), how to show the the letter and count how many vowel and consonant inside it.
Example:
Users input=13
Then will show =
a
b
c
d
e
f
g
h
i
j
k
l
m
Vowel = 3
Consonant = 10
i just now how to count it not to show it
#include <iostream>
using namespace std;
int main (){
int vow=0, con=0;
char let;
.........
.........
if (let=='a' || let=='e' || let=='i' || let=='o' || let=='u'){vow++}
else{con++}
cout<<"Vowel = "<<vow<<endl;
cout<<"Consonant"<<con<<endl;
}
Your if statement does not do what you expect it to. The correct syntax for this is
if (let=='a' || let=='e' || let=='i' || let=='o' || let=='u')
The reason the current versions is incorrect is because it is equivalent to
if ((let=='a') or ('e') or ('i') or ('o') or ('u'))
So 'e', 'i', etc are being evaluated for truthiness and ord to the first condition. Only an empty string in this case will evaluate to false, all of these characters will be true. So your statement evaluates to
if ((let=='a') or true or true or true or true)
Which will always be true.
#include <iostream>
using namespace std;
/**
* Checks if the given letter is a vowel.
*/
bool isVowel(char let) {
return let == 'a' || let == 'e' || let == 'i' || let == 'o' || let == 'u';
}
/**
* Returns the character for the given int.
*/
char toChar(int num) {
return (char) ('a' + num - 1);
}
int main (void) {
int vow = 0,
con = 0,
num,
i;
char let;
cout << "Please enter a number: ";
cin >> num;
for (i = 1; i <= num; ++i) {
let = toChar(i);
if (isVowel(let)) vow++;
else con++;
}
cout << "The letter was \"" << let
<< "\" and there were " << vow
<< " vowels and " << con
<< " consonants." << endl;
return 0;
}
To show the letters, you can use a for loop, using a char as index.
int n = 13;
unsigned int vowel = 0;
unsigned int consonant = 0;
int a = (int)'a';
for (char letter = 'a'; (int)letter < a + n; letter++) {
cout << letter << " ";
if (is_vowel(letter)) vowel++;
else consonant++;
}
cout << std::endl << "vowels: "<< vowel << " consonants: " << consonant << std::endl;
So, you must implement the is_vowel method.
Related
This question already has answers here:
How to cin Space in c++?
(8 answers)
Closed last month.
I'm having problems with how to show the numbers of vowels in the string I input in this code I made
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
char str[100];
int a, e, i, o, u, whitespace;
a = e = i = o = u = whitespace = 0;
int k;
cout << "Enter a string: ";
cin >> str[99];
for (k = 0; k < strlen(str); k++)
{
if (str[k] == 'A')
{
a = a + 1;
}
else if (str[k] == 'E')
{
e = e + 1;
}
else if (str[k] == 'I')
{
i = i + 1;
}
else if (str[k] == 'O')
{
o = o + 1;
}
else if (str[k] == 'U')
{
u = u + 1;
}
else if (str[k] == ' ')
{
whitespace++;
}
}
cout << "\nVowels:\n";
cout << "A " << a;
cout << "\nE " << e;
cout << "\nI " << i;
cout << "\nO " << o;
cout << "\nU " << u;
cout << "\nWhitespaces - " << whitespace;
return 0;
}
I hope I just need to tweak a few things because I'm new to programming lol
I typed "WELCOME TO ARRAYS" but the vowels and whitespace shows 0
The error is here
cin >> str[99];
You must write to the beginning of the array so just pass
cin >> str;
or if you want to be more clear pass the address of the first element
cin >> &str[0];
and str will be filled from the beginning ith the chars you enter.
The >> operator only reads until the first whitespace though, so it would only count vowels of the first word.
So in general cin.getline or std::getline is better as it reads everything
cin.getline(str,sizeof(str));
I've been working on this code for a while now and I keep getting the same terminal error. I have narrowed the issue down to two at functions. I've looked it up but the only answer I can seem to find is if the coder used the wrong variable in a for loop or that the variable in the at function isn't indexed properly.
Can't seem to figure out why the str.at() functions specifically are throwing errors when the variable str should be initialized. The at functions in question are the second and fourth if statements inside the do-while loop.
Here's the code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// start main function
int main()
{
ifstream infile;
string line;
string str;
int words = 0;
int charsNotIncludingSpaces = 0;
int charsIncludingSpaces = 0;
int wordsEndingWithE = 0;
int sixLetterWords = 0;
int wordsBeginningWithVowel = 0;
int wordsContainingATE = 0;
int allEs = 0;
int wordsWithAtleastTwoEs = 0;
infile.open("USDictionary.txt");
// read the first line from the file
getline(infile, line);
while(!infile.eof())
{
unsigned int startIndex = 0;
unsigned int endIndex = 0;
// get each word from the line and find the required results
do
{
endIndex = line.find(' ', startIndex);
if(endIndex > 0 && endIndex < line.size())
{
str = line.substr(startIndex, endIndex - startIndex);
}
else
{
str = line.substr(startIndex);
}
startIndex = endIndex + 1;
words++;
charsNotIncludingSpaces += str.size();
if(str.at(str.size() - 1) == 'e')
{
wordsEndingWithE++;
}
if(str.size() == 6)
{
sixLetterWords++;
}
if(str.at(0) == 'a' || str.at(0) == 'A'
|| str.at(0) == 'e' || str.at(0) == 'E'
|| str.at(0) == 'i' || str.at(0) == 'I'
|| str.at(0) == 'o' || str.at(0) == 'O'
|| str.at(0) == 'u' || str.at(0) == 'U')
{
wordsBeginningWithVowel++;
}
unsigned int ateIndex = str.find("ate");
if (ateIndex >= 0 && ateIndex < str.size())
{
wordsContainingATE++;
}
for (unsigned int k = 0; k < str.size(); k++)
{
if(str.at(k) == 'e')
allEs++;
}
if (str.find_first_of('e') != str.find_last_of('e'))
{
wordsWithAtleastTwoEs++;
}
} while (endIndex > 0 && endIndex < line.size());
charsIncludingSpaces += line.size();
// read the next line from the file
getline(infile, line);
}
infile.close();
/*// print the results
cout << "Total number of words in the dictionary: "
<< words << endl;
cout << "Total number of characters in the dictionary (not including white spaces): "
<< charsNotIncludingSpaces << endl;
cout << "Total number of characters in the dictionary (including white spaces): "
<< charsIncludingSpaces << endl;
cout << "Total number of words ending in the letter e: "
<< wordsEndingWithE << endl;
cout << "Total number of 6 letter words: "
<< sixLetterWords << endl;
cout << "Total number of words beginning with a vowel: "
<< wordsBeginningWithVowel << endl;
cout << "Total number of words containing the substring \"ate\": "
<< wordsContainingATE << endl;
cout << "Total number of occurances of the letter e: "
<< allEs << endl;
cout << "Total number of words containing at least two occurances of the letter e: "
<< wordsWithAtleastTwoEs << endl;*/
return 0;
}
I am learning C++. I am having a problem where I am allocating memory without the new keyword. The memory leak starts from UserInputCharArray[size - 1] = aChar. I am assigning a character to a memory location which is not allocated using the new keyword.
Can anyone help me solve this issue to allocate memory using the new keyword?
Here is an image when I run:
Here is my code:
#include <iostream>
using namespace std;
// Creating a function for high frequency letters by passing in our vowels and
// a boolean for moving from lower-case vowels to upper-case vowels.
void highestFreq(int a, int e, int i, int o, int u, bool isLow) {
// Declaring variables.
int maximumCount = 0;
char mainChar;
// Creating an array of five for the vowels.
int arr[5];
arr[0] = a;
arr[1] = e;
arr[2] = i;
arr[3] = o;
arr[4] = u;
// Set counter to zero.
int counter = 0;
// Looping through the vowels starting with value 1 -> 5.
for (int x = 1; x < 5; x++) {
// Count when array is less than
if (arr[counter] < arr[x])
counter = x;
}
// Looping through the vowels starting with value 0 -> 5
for (int x = 0; x < 5; x++) {
// count plus one.
if (arr[counter] == arr[x])
maximumCount += 1;
}
// Checking with the vowels.
if (counter == 0)
mainChar = 'a';
else if (counter == 1)
mainChar = 'e';
else if (counter == 2)
mainChar = 'i';
else if (counter == 3)
mainChar = 'o';
else if (counter == 4)
mainChar = 'u';
// Moving from lower case letter to upper case letter.
if (!isLow)
// Conversion happening here from lower-case to upper-case letters.
mainChar = mainChar - 'a' + 'A';
// If there are multiple high frequency letters then a message needs to be passed in there.
if (maximumCount > 1) {
// checking to see if there are no lower case vowels .
if (isLow)
// Display - if there is no lower case vowels or there is no
// upper case vowels.
cout << "There are no lower ";
else
// Otherwise - display there are no upper case vowels
cout << "There are no upper ";
cout << "case vowels" << "\n";
}
else {
// Otherwise - display the highest frequency letters.
cout << "The highest frequency ";
// Checking to see if there is any high frequency letters.
if (isLow)
// Displaying lower case vowels
cout << "lower ";
else
// Displaying upper case vowels
cout << "upper ";
// Display how many high frequency vowels are their.
cout << "case vowel is " << mainChar << " with a frequency of " << arr[counter] << "\n";
}
}
int main() {
// A char is being used - for the user to still continue with the program.
char userInputProceed = 'y';
// Looping begin for user to continue.
while (userInputProceed == 'Y' || userInputProceed == 'y') {
// Declaration for the lower-case and upper-case vowels.
int low_a, low_e, low_i, low_o, low_u;
int high_A, high_E, high_I, high_O, high_U;
// Initializing the count of lower-case and upper-case vowels.
low_a = low_e = low_i = low_o = low_u = 0;
high_A = high_E = high_I = high_O = high_U = 0;
// Setting a size starting from one.
int size = 1;
// Declaring a variable of type char.
char aChar;
// Declaring a size to be using the new operator.
char *UserInputCharArray = new char[size];
// Prompt the user to enter a string.
cout << "Enter a string: ";
// Capturing userInput, one char at a time.
aChar = cin.get();
// A loop that will be taking values from the input stream and it will
// count the size of input. \n
while (aChar != 10) {
// Count size of input.
UserInputCharArray[size - 1] = aChar;
// Getting each size and add one.
size += 1;
// Then get the char one at a time - once more.
aChar = cin.get();
}
// The loop process of the vowels.
for (int x = 0; x < size; x++) {
aChar = *(UserInputCharArray + x);
if (aChar - 'a' == 0)
low_a += 1;
else if (aChar - 'e' == 0)
low_e += 1;
else if (aChar - 'i' == 0)
low_i += 1;
else if (aChar - 'o' == 0)
low_o += 1;
else if (aChar - 'u' == 0)
low_u += 1;
else if (aChar - 'A' == 0)
high_A += 1;
else if (aChar - 'E' == 0)
high_E += 1;
else if (aChar - 'I' == 0)
high_I += 1;
else if (aChar - 'O' == 0)
high_O += 1;
else if (aChar - 'U' == 0)
high_U += 1;
}
// Displaying lower-case vowels with their frequency and display - following with
// if the count of any vowel is zero then it will not be displayed.
if (low_a > 0 || low_e > 0 || low_i > 0 || low_o > 0 || low_u > 0) {
cout << "The lower-case vowels which are present are: ";
if (low_a > 0)
cout << "a (" << low_a << ")";
if (low_e > 0)
cout << "e (" << low_e << ")";
if (low_i > 0)
cout << "i (" << low_i << ")";
if (low_o > 0)
cout << "o (" << low_o << ")";
if (low_u > 0)
cout << "u (" << low_u << ")";
cout << endl;
}
// Displaying upper case vowels with their frequency and display - following with
// if the count of any vowel is zero then it will not be displayed.
if (high_A > 0 || high_E > 0 || high_I > 0 || high_O > 0 || high_U > 0) {
cout << "The upper-case vowels which are present are: ";
if (high_A > 0)
cout << "A (" << high_A << ")";
if (high_E > 0)
cout << "E (" << high_E << ")";
if (high_I > 0)
cout << "I (" << high_I << ")";
if (high_O > 0)
cout << "O (" << high_O << ")";
if (high_U > 0)
cout << "U (" << high_U << ")";
cout << endl;
}
// Calling out the high frequency letter function.
highestFreq(low_a, low_e, low_i, low_o, low_u, true);
highestFreq(high_A, high_E, high_I, high_O, high_U, false);
// Using the delete operator.
delete[] UserInputCharArray;
// Waiting for the user input if they want to continue with the program.
cout << "To continue enter y or Y, anything else will quit the program" << endl;
// Getting the user input to continue with the program.
cin >> userInputProceed;
// Wait for userInput until he/she quits.
cin.get();
}
return 0;
}
Updated Code
#include <iostream>
#include <string>
using namespace std;
// ----- REFACTORED MY CODE. ------
// Returning a lower case vowels.
char LowVowel(int x) {
switch(x) {
case 0:
return 'a';
break;
case 1:
return 'e';
break;
case 2:
return 'i';
break;
case 3:
return 'o';
break;
case 4:
return 'u';
break;
}
}
// Returning a upper case vowels.
char UpperVowel(int x) {
switch (x) {
case 0:
return 'A';
break;
case 1:
return 'E';
break;
case 2:
return 'I';
break;
case 3:
return 'O';
break;
case 4:
return 'U';
break;
}
}
// Initializing the array to be used for upper and lower cases frequency.
void init(int arr[]) {
for (int x = 0; x < 5; x++) {
arr[x] = 0;
}
}
int main() {
// Declaring a variable of type char
char aChar;
// Allocation of memory to a character pointer as per the size of
// the entered input.
char *cPointer = new char;
// Store an array for the frequency of lower and upper vowels.
int arr[5];
// counter and maximum to be stored for our maximum frequency.
int counter = 0, maximum;
// Prompting the user to enter a string.
cout << "Enter a string: ";
// Capturing userInput.
aChar = cin.get();
while (aChar != '\n') {
// increment counter.
counter++;
// then if enter key is pressed - stop entering data.
if (aChar == '\n')
break;
// Storing the accepted char inside the pointer.
*(cPointer + counter) = aChar;
// Increase the counter.
aChar = cin.get();
}
// Initializing the arr array
init(arr);
// Looping until the end of inputted information.
for (int x = 0; x < counter; x++) {
// Checking the lower case vowels
// and even increase the counter
if (*(cPointer + x) == 'a')
arr[0]++;
else if (*(cPointer + x) == 'e')
arr[1]++;
else if (*(cPointer + x) == 'i')
arr[2]++;
else if (*(cPointer + x) == 'o')
arr[3]++;
else if (*(cPointer + x) == 'u')
arr[4]++;
}
// Display lower case vowels results.
cout << "The lower-case vowels which are present are: \n ";
// Looping..
for (int x = 0; x < 5; x++) {
// find the maximum frequency.
maximum = arr[x];
if (arr[x] > maximum)
maximum = x;
// then if the frequency value not zero then
// display.
if (arr[x] != 0) {
cout << LowVowel(x)
<< " ( " << arr[x] << " )";
}
}
// Display lower case vowels.
cout << "Lower case vowel " << LowVowel(maximum)
<< " which appears most frequently = " << arr[maximum];
// init the arr array .
init(arr);
// looping once again for the entered info.
for (int x = 0; x < counter; x++) {
// checking the upper case vowels
// and increasing the counter once again.
if (*(cPointer + x) == 'A')
arr[0]++;
else if (*(cPointer + x) == 'E')
arr[1]++;
else if (*(cPointer + x) == 'I')
arr[2]++;
else if (*(cPointer + x) == 'O')
arr[3]++;
else if (*(cPointer + x) == 'U')
arr[4]++;
}
// Display upper case vowels.
cout << "Upper case vowels and its frequency" << endl;
// looping
for (int x = 0; x < 5; x++) {
// finding the maximum freq.
maximum = arr[x];
if (arr[x] > maximum)
maximum = x;
// if the frequency value is not zero then display.
if (arr[x] != 0) {
cout << "\n Vowel " << UpperVowel(x) << " occurred " << arr[x] << " times";
}
}
cout << "Upper case vowel " << UpperVowel(maximum) << " which appears most frequently = " << arr[maximum];
}
I am assigning a character to a memory location which I am not even
allocated memory using the 'new' keyword.
Actually, you have allocated memory for that location... Specifically, you declare and allocate 1-char of data with:
int size = 1;
char *UserInputCharArray = new char[size];
So the pointer UserInputCharArray points to a valid block of memory of size 1. The value at that memory address can be accessed by dereferencing the pointer with either:
*UserInputCharArray;
or
UserInputCharArray[0]; (or UserInputCharArray[size-1] as you used)
(the [] operator acts as a dereference.)
Both are equivalent. However, when using [], you are limited to the single index 0. Anything more (e.g. 1) would invoke Undefined Behavior as an attempt to access memory outside the bounds of the block you allocated. (as you do in your loop beginning when size = 2)
Why Not Use string?
While you are free to manage the storage needs for UserInputCharArray your self with new and delete, you are far better served using the features of C++ (namely the string handling provides by #include <string>) that will provide automatic memory management for UserInputCharArray, e.g. you can #include <string> and change:
char *UserInputCharArray = new char[size];
to
string UserInputCharArray;
and then:
UserInputCharArray[size - 1] = aChar;
to
UserInputCharArray.push_back(aChar);
The memory will then also be freed when UserInputCharArray goes out of scope.
Note, you have additional logic errors with your code.
I would like to to print a triangle with a given letter. For example, if I input D, the program should return:
A
AB
ABC
ABCD
So far, I have managed to print all letters until the given one in my example, but as you see this method is not quite effective since I need to do this for all 26 cases since the English alphabet is 26 chars. Is there some way to optimize my code?
#include <iostream>
using namespace std;
int main() {
char i;
cout << "Enter char ";
cin >> i;
int c = static_cast<int>(i);
if (65 < c) {
cout << "A";
cout << endl;
}
if (66 < c) {
cout << "AB";
cout << endl;
}
if (67 < c) {
cout << "ABC";
cout << endl;
}
for (int i = 64; i < c; i++) {
cout << static_cast<char>(i + 1);
}
return 0;
}
You definitely need to work on your comprehension of loops. This one works just fine and it even has some checks on what is typed in and it eventually converts lower case letters into upper casse.
char first = 'A';
char last = 0;
cout << "Enter a char: ";
cin >> last;
fflush(stdin);
cout << "\n\n";
if ((last > 96) && (last < 123)) //97 to 122 are lower case letters
{
last -= 32; //32 is the delta between each lower case letter and its upper case "twin"
}
if ((last > 64) && (last < 91))
{
for (char i = 65; i <= last; i++)
{
for (char j = 65; j <= i; j++)
{
cout << j;
}
cout << "\n";
}
}
else
{
cout << "\nWrong character!!\n\n";
return 0;
}
Use a nested loop structure. Use the outer loop to 'walk' down your triangle,
lineLength = 1;
while(lineLength <= (c - 64)){
...stuff...
lineLength++;
cout << endl;
}
Use the inner loop to 'walk' down the alphabet (you've already done most of this):
for (int i = 0; i < lineLength; i++) {
cout << static_cast<char>(i + 65);
}
Putting it together:
lineLength = 1;
while(lineLength <= (c - 64)){
for (int i = 0; i < lineLength; i++) {
cout << static_cast<char>(i + 65);
}
lineLength++;
cout << endl;
}
I see that someone else has posted a similar answer. Between these two answers, you should be able to find your way. I haven't compiled and run this code, but I believe that it should work or be very close.
Don't harcode ascii integer values into code. Explicitly use the character or string literals (e.g. 'A' instead of 65)
Start with a helper function to print exactly one line
// prints all the characters of the alphabetic sequence from "A" to the final char designated by <c>
void printTriangleLine(char c)
{
if ((c < 'A') || (c > 'Z'))
{
return;
}
for (char x = 'A'; x <= c; x++)
{
cout << x;
}
cout << endl;
}
Then put it all together in your main:
int main()
{
char i;
cout << "Enter char ";
cin >> i;
if ((i < 'A') || (i > 'Z'))
{
return 0;
}
for (char x = 'A'; x <= i; x++)
{
printTriangleLine(x);
}
return 0;
}
We must run the loop from position is above 'A' character
until we reached the charanter you enter
// procead until reached input letter
while (chNew != c)
{
// go to next letter
chNew++;
// start with 'A' until current char + 1
for (int j = 'A'; j < chNew + 1; j++)
cout << (char)j;
// go to next line
cout << endl;
}
in each loop we increment character value by 1 to go to the next value
// go to next letter
chNew++;
inner loop simply print the character from A to next value relative to current chNew + 1, it is because we also want to include current character to our printed line.
Here is your working code.
#include <iostream>
using namespace std;
int main()
{
char i;
cout << "Enter char ";
cin >> i;
int c = static_cast<int>(i);
// start with 'A' - 1 character
char chNew = 'A' - 1;
// procead until reached input letter
while (chNew != c)
{
// go to next letter
chNew++;
// start with 'A' until current char + 1
for (int j = 'A'; j < chNew + 1; j++)
cout << (char)j;
// go to next line
cout << endl;
}
// we have done
return 0;
}
I need a little help figuring out a couple of parts from a C++ assignment. I am asked to write a program as follows:
Write a program that accepts input from the keyboard (with the input
terminated by pressing the Enter key) and counts the number of letters (A-Z and a-z), numerical digits (0-9), and other characters. Input the string using cin and use the following looping structure to examine each character in the string with an "if" statement and multiple "else if" statements.
char s[50];
int i;
. . .
i = 0;
while (s[i] != 0) { // a string is terminated with a null (0) value
. . .
i++;
}
Your program should make use of the relational operators (e.g., == < > <= >= !=) to determine whether a particular character is a letter, number, or other character. You may only #include and
may not use any other include files.
The program should have an output similar to the following:
Enter a continuous string of characters with no blank spaces (example: aBc1234!##$%)
enter your string: aBc1234!##$%
your string has 12 total characters
3 letters
4 numerical characters
5 other characters
Here is an example program that counts lower case letters:
// PROG07.CPP example
#include <iostream>
using namespace std;
int main()
{
char s[50];
int i;
int lowercase = 0;
//get string from the user
cout << "Enter a continuous string of characters with no blanspaces\n"
cout << "(example: aBc1234!##$%)" << endl << endl;
cout << "Enter your string: ";
cin >> s;
cout << endl;
// loop through the string, lower case letters
// note, strings (character arrays) have an invisible
// zero value at their end
i = 0;
while (s[i] != 0) // while the character does not have ASCII code zero
{
if ((s[i] >= 'a' && s[i] <= 'z'))
lowercase++;
i++;
}
cout << "Your string has " << lowercase << " lower case letters" << endl;
// including the next line for Dev-C++:
system("pause"); // not needed for CodeBlocks
return 0;
}
So far, I have come up with this:
#include <iostream>
using namespace std;
int main()
{
char s[50];
int i;
int lowercase, uppercase, numChars, otherChars = 0;
cout << "Enter a continuous string of characters" << endl;
cout << "(example: aBc1234!##$%)" << endl;
cout << "Enter your string: ";
cin >> s;
cout << endl;
while (s[i] != 0) // while the character does not have ASCII code zero
{
if ((s[i] >= 'a' && s[i] <= 'z'))
lowercase++;
i++;
}
while (s[i] != 0)
{
if ((s[i] >= 'A' && s[i] <= 'Z'))
uppercase++;
i++;
}
cout << lowercase + uppercase << " letters" << endl;
i = 0;
while (s[i] != 0)
{
if ((s[i] >= '0' && s[i] <= '9'))
numChars++;
i++;
}
cout << numChars << " numerical characters" << endl;
return 0;
}
Any help would be greatly appreciated.
You have to reset i to 0 before every loop:
#include <iostream>
using namespace std;
int main()
{
char s[50];
int i;
int lowercase, uppercase, numChars, otherChars = 0;
cout << "Enter a continuous string of characters" << endl;
cout << "(example: aBc1234!##$%)" << endl;
cout << "Enter your string: ";
cin >> s;
cout << endl;
i = 0; //missing
while (s[i] != 0) // while the character does not have ASCII code zero
{
if ((s[i] >= 'a' && s[i] <= 'z'))
lowercase++;
i++;
}
i = 0; // missing
while (s[i] != 0)
{
if ((s[i] >= 'A' && s[i] <= 'Z'))
uppercase++;
i++;
}
cout << lowercase + uppercase << " letters" << endl;
i = 0;
while (s[i] != 0)
{
if ((s[i] >= '0' && s[i] <= '9'))
numChars++;
i++;
}
cout << numChars << " numerical characters" << endl;
return 0;
}
Looks good so far, just a few things
First off, you only need the one while loop:
while (s[i] != 0)
{
//All your if checks can go in here
}
Then, according to the output you need, you will need 4 variables:
int total, lettters, numbers, otherCharacters;
At the start of your loop, add to total:
while (s[i] != 0)
{
total++;
}
Then you will need 3 if checks inside your while loop, one for letters, one for numbers, and one for other characters:
if ((s[i] > 'a' && s[i] < 'z') || (s[i] > 'A' && s[i] < 'Z')) { ... }
else if (s[i] > '0' && s[i] < '9') { ... }
else { ... }
Then just output all your variables according to the output you mentioned:
cout << "your string has " << total << " total characters, " << letters << " letters, " << numbers << " numerical characters, and " << otherCharacters << " characters.";