-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA29c-strings2.cpp
113 lines (97 loc) · 2 KB
/
A29c-strings2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// C-Strings Pt.2
// By Alina Corpora, Emily Dayanghirang, and Sarra Osman
#include <iostream>
#include <cctype>
using namespace std;
// Function Prototypes
int words(char *cStr);
int nums(char *cStr);
int spaces(char *cStr);
int puncts(char *cStr);
int consts(char *cStr);
// Global Variables
const int SIZE = 51;
int main()
{
char cStr[SIZE];
cout << "Enter a string input containing no more than "
<<(SIZE-1)<<" characters. : ";
cin.getline(cStr, SIZE);
int wordC=words(cStr);
cout<<"Words: "<<wordC<<endl;
int numC=nums(cStr);
cout<<"Numbers: "<<numC<<endl;
int spaceC=spaces(cStr);
cout<<"Spaces: "<<spaceC<<endl;
int punctC=puncts(cStr);
cout<<"Punctuations:"<<punctC<<endl;
int constC=consts(cStr);
cout<<"Consonants: "<<constC<<endl;
return 0;
}
// Count words
int words(char *cStr)
{
int count = 0;
while(*cStr != '\0')
{
if((ispunct(*cStr) || isspace(*cStr)) && (isalpha(*(cStr+1)) ||
isdigit(*(cStr+1))))
count++;
cStr++;
}
return count;
}
//Count Numbers
int nums(char *cStr)
{
int count = 0;
while(*cStr != '\0')
{
if (isdigit(*cStr))
count++;
cStr++;
}
return count;
}
// Count Spaces
int spaces(char *cStr)
{
int count = 0;
while(*cStr !='\0')
{
if(isspace(*cStr))
count++;
cStr++;
}
return count;
}
// Count punctuations
int puncts(char *cStr)
{
int count = 0;
while(*cStr != '\0')
{
if(ispunct(*cStr))
count++;
cStr++;
}
return count;
}
// Count consonants
int consts(char *cStr)
{
int count = 0;
while(*cStr != '\0')
{
if (isalpha(*cStr))
{
char upCase = toupper(*cStr);
if (upCase != 'A' && upCase != 'E' && upCase != 'I' && upCase != 'O'
&& upCase != 'U')
count++;
}
cStr++;
}
return count;
}