-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter_counter.c
48 lines (38 loc) · 1.32 KB
/
character_counter.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
bool continueProgram = true;
char choice;
// Main program loop
while (continueProgram) {
int characterCount = 0;
char currentChar;
printf("\n===== CHARACTER COUNTER =====\n\n");
printf("Please write a word or sentence (press Enter when done):\n");
// Reset input buffer
fflush(stdin);
// Count characters until Enter key (newline) is pressed
while ((currentChar = getchar()) != '\n') {
// Only count if not at EOF (handles Ctrl+D or similar)
if (currentChar != EOF) {
characterCount++;
} else {
break; // Exit if EOF encountered
}
}
printf("\nYou entered %d characters.\n", characterCount);
// Ask if user wants to continue
printf("\nDo you want to count more characters? (y/n): ");
scanf(" %c", &choice);
// Clear input buffer after scanf
while (getchar() != '\n');
// Check if user wants to exit
if (choice != 'y' && choice != 'Y') {
continueProgram = false;
}
}
printf("\nThank you for using the Character Counter program!\n");
return 0;
}