-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadprintxt.c
89 lines (71 loc) · 1.81 KB
/
readprintxt.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
#include <stdio.h>
#include <ctype.h>
void printString(char *str);
int countCharacters(char *str);
int countWords(char *str);
int countSentences(char *str);
int main() {
char str[1000];
// Get the string from the user
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Print the string
printf("The string is: ");
printString(str);
// Count the number of characters
int numChars = countCharacters(str);
printf("\nNumber of characters: %d\n", numChars);
// Count the number of words
int numWords = countWords(str);
printf("Number of words: %d\n", numWords);
// Count the number of sentences
int numSentences = countSentences(str);
printf("Number of sentences: %d\n", numSentences);
return 0;
}
// Function to print a string using pointers
void printString(char *str) {
while (*str != '\0') {
printf("%c", *str);
str++;
}
}
// Function to count the number of characters in a string using pointers
int countCharacters(char *str) {
int count = 0;
while (*str != '\0') {
if (*str != '\n') {
count++;
}
str++;
}
return count;
}
// Function to count the number of words in a string using pointers
int countWords(char *str) {
int count = 0;
int inWord = 0;
while (*str != '\0') {
if (isalpha(*str)) {
if (!inWord) {
inWord = 1;
count++;
}
} else {
inWord = 0;
}
str++;
}
return count;
}
// Function to count the number of sentences in a string using pointers
int countSentences(char *str) {
int count = 0;
while (*str != '\0') {
if (*str == '.' || *str == '!' || *str == '?') {
count++;
}
str++;
}
return count;
}