-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings-01.c
157 lines (145 loc) · 4.06 KB
/
strings-01.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/********************************************
* Write a program that enables the user to input a string.
* The variable takes a maximum of 100 characters including the NULL character.
* Use the 'character by character' method.
* Count the occurrences of a digit
* Count the occurrences of a single digit in the written string
* Count the occurrences of all letters in the written string
* Count the occurrences of all capital letters in the written string
* Count the occurrences of all lower case letters in the written string
********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define len 100
int inputRow(char *string) {
char c = 'x', i = 0;
while (c != '\n' && i < len - 1) {
c = getchar();
string[i] = c;
i++;
}
string[i - 1] = '\0';
return i;
}
int digitNumber(char *string) {
int digit_number = 0, i = 0;
while (string[i] != '\0') {
if (isdigit(string[i])) {
digit_number++;
}
i++;
}
return digit_number;
}
void singleDigitNumber(char *string) {
int digit_number[10] = {0};
int i = 0;
while (string[i] != '\0') {
int index = string[i] - '0';
if (index >= 0 && index <= 9) {
digit_number[index]++;
}
i++;
}
for (i = 0; i < 10; i++) {
if (digit_number[i] != 0) {
printf("The digit %d is repeated %d times.\n", i, digit_number[i]);
}
}
}
int countLetters(char *string) {
int i = 0;
int letter_number = 0;
while (string[i] != '\0') {
if (isalpha(string[i])) {
letter_number++;
}
i++;
}
return letter_number;
}
int countCapitalLetters(char *string) {
int i = 0;
int letter_number = 0;
while (string[i] != '\0') {
if (isupper(string[i])) {
letter_number++;
}
i++;
}
return letter_number;
}
int countLowerLetters(char *string) {
int i = 0;
int letter_number = 0;
while (string[i] != '\0') {
if (islower(string[i])) {
letter_number++;
}
i++;
}
return letter_number;
}
void countSelfCapitalLetters(char *string) {
int letter_number[26] = {0};
int i = 0;
while (string[i] != '\0') {
int index = string[i] - 'A';
if (index >= 0 && index < 26) {
letter_number[index]++;
}
i++;
}
for (i = 0; i < 26; i++) {
if (letter_number[i] != 0) {
printf("The letter %c is repeated %d times.\n", 'A' + i, letter_number[i]);
}
}
}
void countSelfLowerLetters(char *string) {
int letter_number[26] = {0};
int i = 0;
while (string[i] != '\0') {
int index = string[i] - 'a';
if (index >= 0 && index < 26) {
letter_number[index]++;
}
i++;
}
for (i = 0; i < 26; i++) {
if (letter_number[i] != 0) {
printf("The letter %c is repeated %d times.\n", 'a' + i, letter_number[i]);
}
}
}
void countSelfLetters(char *string) {
int letter_number[26] = {0};
int i = 0;
while (string[i] != '\0') {
int index = toupper(string[i]) - 'A';
if (index >= 0 && index < 26) {
letter_number[index]++;
}
i++;
}
for (i = 0; i < 26; i++) {
if (letter_number[i] != 0) {
printf("The letter %c (or %c) is repeated %d times.\n", 'A' + i, 'a' + i, letter_number[i]);
}
}
}
int main() {
char string[len];
printf("Please input a string: ");
inputRow(string);
printf("The number of digits in the string is: %d\n", digitNumber(string));
singleDigitNumber(string);
printf("The number of letters in the string is: %d\n", countLetters(string));
printf("The number of lowercase letters in the string is: %d\n", countLowerLetters(string));
printf("The number of uppercase letters in the string is: %d\n", countCapitalLetters(string));
countSelfCapitalLetters(string);
countSelfLowerLetters(string);
countSelfLetters(string);
return EXIT_SUCCESS;
}