I need help writing a program that counts the number of letters, numbers, and special characters independently while only using iostream. Here is what I have so far. I have only been able to get it to count number and letters but i am not quite sure how to go about counting special characters.
Quit spitting out code, TheSmallGuy, and give some explanation.
I'm not sure what characters would count as 'special' characters, but you can consult the ASCII table http://www.asciitable.com/ to see what the ASCII value is for a specific character. If you want to compare with it, you can cast the char to an int and compare it with the value in the ASCII table
Or if you just want any character that isn't '0' - '9', 'a' - 'z', or 'A' - 'Z' to be special then TheSmallGuy's code would work.
special characters are characters that that are not a letter or a number such as ! or $. Their Ascii numbers fall between the numbers for capital and lowercase letters in some instances.
Here is my code revised and explained.
#include <iostream>
usingnamespace std;
int main()
{
char s[50];
int i = 0; // used to help the program know when to end
int lc = 0; // holds the number of lowercase letters
int uc = 0; // holds the number of uppercase letters
int numbers = 0; // holds the amount of numbers
int special = 0; // holds the number of special characters.
cout << "Enter a continuous string of characters with no blank spaces.\n " << endl;
cin >> s; // obtains the string
cout << endl;
while (s[i] !=0) // creates a conditional loop
{
if (s[i] >= 'a' && s[i] <= 'z')
{lc++;} // counts the number of lowercase letters
else
{if (s[i] >= 'A' && s[i] <= 'Z')
{uc++;} // counts uppercase letters after all lowercase letters have been counted
else {if (s[i] >= '0' && s[i] <= '9') // counts numbers after uppercase letters.
{numbers++;}
// I am not certain how to go about counting special characters therefore it is left blank
}
}
i++;
}
cout << "There are " << lc+uc << " letters, " << numbers << " number(s), and " << special << " special characters" << endl;
// displays the total number of letters and numbers and eventually special characters once i figure out how to do so.
return 0;
}
I'm assume there is a simpler way of doing this rather than making 5 million if/else statements. If that is the only way then I guess i will give it a shot.
// PROG07.CPP Mark Johnson Counts characters in a string.
#include <iostream>
usingnamespace std;
int main()
{
char s[50]; // Creates string.
int i = 0; // Creates a variable to allow the while loop to know when to stop.
int lc = 0; // Each variable allows for the program to
int uc = 0; // insert a total value for each type of character
int numbers = 0; // after the while loop has finished.
int special1 = 0;// 4 of theses are required due to the ASCII
int special2 = 0;// numerical system having values for special
int special3 = 0; // characters mixed along side values for
int special4 = 0; // letters and numbers.
cout << "Enter a continuous string of characters with no blank spaces.\n " << endl;
// ask user for string and stores in "s"
cin >> s;
cout << endl;
while (s[i] !=0) // while loop that runs until 0 is reached
{
if (s[i] >= 'a' && s[i] <= 'z')//calculates lowercase characters
{lc++;}
else
{if (s[i] >= 'A' && s[i] <= 'Z')//calculates uppercase characters
{uc++;}
else {if (s[i] >= '0' && s[i] <= '9')//calculates numbers characters
{numbers++;}
else {if (s[i] >= 32 && s[i] <= 47) //calculates first set of values containing special characters.
{special1++;}
else {if (s[i] >= 58 && s[i] <= 64) // calculates second
{special2++;}
else {if (s[i] >= 91 && s[i] <= 96) // calculates third
{special3++;}
else {if (s[i] >= 123 && s[i] <= 126) // calculates fourth
{special4++;}
}
}
}
}
}
}
i++; // allows while loop to end
}
int sp = special1 + special2 + special3 + special4; // adds special character values
int total = lc + uc + numbers + sp; // adds total values
cout << "There are " << total << " total characters in the string." << endl;
cout << "There are " << lc+uc << " letter(s), " << numbers << " number(s), and " << sp << " special character(s)." << endl;
return 0;
}
// PROG07.CPP Mark Johnson Counts characters in a string.
#include <iostream>
usingnamespace std;
int main()
{
char s[50]; // Creates string.
int i = 0; // Creates a variable to allow the while loop to know when to stop.
int lc = 0; // Each variable allows for the program to
int uc = 0; // insert a total value for each type of character
int numbers = 0; // after the while loop has finished.
int special1 = 0;// 4 of theses are required due to the ASCII
int special2 = 0;// numerical system having values for special
int special3 = 0; // characters mixed along side values for
int special4 = 0; // letters and numbers.
cout << "Enter a continuous string of characters with no blank spaces.\n " << endl;
// ask user for string and stores in "s"
cin >> s;
cout << endl;
while (s[i] != 0) // while loop that runs until 0 is reached
{
if (s[i] >= 'a' && s[i] <= 'z')//calculates lowercase characters
{
lc++;
}
else
{
if (s[i] >= 'A' && s[i] <= 'Z')//calculates uppercase characters
{
uc++;
}
else {
if (s[i] >= '0' && s[i] <= '9')//calculates numbers characters
{
numbers++;
}
else {
if (s[i] >= 32 && s[i] <= 47) //calculates first set of values containing special characters.
{
special1++;
}
else {
if (s[i] >= 58 && s[i] <= 64) // calculates second
{
special2++;
}
else {
if (s[i] >= 91 && s[i] <= 96) // calculates third
{
special3++;
}
else {
if (s[i] >= 123 && s[i] <= 126) // calculates fourth
{
special4++;
}
}
}
}
}
}
}
i++; // allows while loop to end
}
int sp = special1 + special2 + special3 + special4; // adds special character values
int total = lc + uc + numbers + sp; // adds total values
cout << "There are " << total << " total characters in the string." << endl;
cout << "There are " << lc + uc << " letter(s), " << numbers << " number(s), and " << sp << " special character(s)." << endl;
return 0;
}
my apologies, i am new to this cite and I am new to C++. I'm am not quite sure how to use code tags but i will learn. I also did not want to come across as rude I just felt that I needed to further explain my situation. Also, some of the formattings of the program is due to the format that I am required to keep it in by my instructor and may be because i simply don't understand