From 8ee8999991aabca403d5ef4d80a8f035b660a0b4 Mon Sep 17 00:00:00 2001 From: Areeba Muzammil <73306700+areebamuzammil@users.noreply.github.com> Date: Mon, 10 Oct 2022 00:37:30 +0500 Subject: [PATCH] Create CharCountInString.cpp --- programs/cpp/CharCountInString.cpp | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 programs/cpp/CharCountInString.cpp diff --git a/programs/cpp/CharCountInString.cpp b/programs/cpp/CharCountInString.cpp new file mode 100644 index 0000000..06c2104 --- /dev/null +++ b/programs/cpp/CharCountInString.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +using namespace std; + +int main() +{ + char* array_point; + char c1; + int count = 0, alp = 0, digt = 0, spcchr = 0, oth = 0; + char string_array[100]; + string str1; + + cout << "\n\n Count the letters, spaces, numbers and other characters:\n"; + cout << "-------------------------------------------------------------\n"; + cout << " Enter a string: "; + getline(cin, str1); + cout << endl; + strcpy_s(string_array, str1.c_str()); + for (array_point = string_array; *array_point != '\0'; array_point++) + { + c1 = *array_point; + count++; + if (isalpha(c1)) + { + alp++; + } + else + if (isdigit(c1)) + { + digt++; + } + else + if (isspace(c1)) + { + spcchr++; + } + else + { + oth++;; + } + } + cout << " The number of characters in the string is: " << count << endl; + cout << " The number of alphabets are: " << alp << endl; + cout << " The number of digits are: " << digt << endl; + cout << " The number of spaces are: " << spcchr << endl; + cout << " The number of other characters are: " << oth << endl << endl; + return 0; +} \ No newline at end of file