Posted
//write down a program that accepts 50 char values from user. The value can be anything char from the
key board. The program counts capitals, smalls and numeric values (0-9) separately, . It has to use a
switch structure to increment counts. It has to use an if else to decide b/w different char types It
displays the count each type at the end.
//now write it using a function, the function determines the type of the character and
returns an integer appropriately e.g. 1 for capitals, 2 for smalls, 3 for numeric values and -1 for all
others. rest of the program lies in the main.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int numerical_count{};
int uppercase_count{};
int lowercase_count{};
char ch = 50;
int i;
std::string result;
result.resize(50);
for (i = 0; i < 50; i++)
{
std::cin >> result[i];
for (const auto& ch : result)
{
if (ch >= '0' && ch <= '9')
numerical_count++;
else if (ch >= 'A' && ch <= 'Z')
uppercase_count++;
else if (ch >= 'a' && ch <= 'z')
lowercase_count++;
}
std::cout << "# numerical: " << numerical_count << std::endl;
std::cout << "# uppercase: " << uppercase_count << std::endl;
std::cout << "# lowercase: " << lowercase_count << std::endl;
}
switch (ch)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
numerical_count++;
break;
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y':
case 'Z':
uppercase_count++;
break;
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y':
case 'z':
lowercase_count++;
break;
}
return 0;
}
https://cdn.discordapp.com/attachments/1088161134621773975/1088481077401751552/Untitled.png
Replied
What the hell
Cancel
Post
"Questionable intellegence, but I like the mystery" - CubeFaces
https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png
Replied
Excise me, have you ever heard of code blocks?
Cancel
Post
Random quote here...
Users viewing this thread:
( Members: 0, Guests: 1, Total: 1 )
Cancel
Post