1
0
Fork 0
This repository has been archived on 2021-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
uni-m.cds-adv-prog/ex1/ex1.2/string_analyzer.cpp

50 lines
1.1 KiB
C++
Raw Normal View History

#include <iostream>
using namespace std ;
#define MAX_STR_LENGTH 50
int main() {
char str[MAX_STR_LENGTH];
// set pointer to beginning of str
char* ptr = &str[0];
// get string from user
std::cout << "Enter string (N < " << MAX_STR_LENGTH << ") :";
std::cin.getline (str, MAX_STR_LENGTH);
std::cout << std::endl;
// loop while the pointer does not point to a null byte
// move the pointer one position at the end of the loop
unsigned int chars_capital = 0, chars_lower = 0, chars_digits = 0, chars_other = 0;
while ( *ptr != 0 )
{
if ( *ptr >= int('A') && *ptr <= int('Z') )
{
++chars_capital;
}
else if ( *ptr >= int('a') && *ptr <= int('z') )
{
++chars_lower;
}
else if ( *ptr >= int('0') && *ptr <= int('9') )
{
++chars_digits;
}
else
{
++chars_other;
}
// move pointer one char further
++ptr;
}
std::cout << chars_capital << " Uppercase" << std::endl;
std::cout << chars_lower << " Lowercase" << std::endl;
std::cout << chars_digits << " Digits" << std::endl;
std::cout << chars_other << " Other" << std::endl;
return 0 ;
}