Question: I am having two issues with my code: 1) it counts numbers as letters, so the average word count is off 2) the autocorrect function
I am having two issues with my code:
1) it counts numbers as letters, so the average word count is off
2) the autocorrect function adds a period even if the input already had one at the end
Sample input:
22 is a cool number. 44 is also cool.
50
center
yes
Correct output:
--------------------------------------------------
22 is a cool number. 44 is also cool.
Words: 9
Avg word length: 2.555556
Here is my code:
#include
#include
#include
#include
int main() {
char input[200];
int width;
char align[50];
char autoC[50];
printf("Input: ");
fgets(input, 200, stdin);
input[strcspn(input, " ")] = '\0'; // remove trailing newline
printf("Column width: ");
scanf("%d", &width);
printf("Alignment: ");
scanf("%s", align);
printf("Autocorrect: ");
scanf("%s", autoC);
if (width < strlen(input)) {
printf("Invalid column width ");
return 1;
}
int i;
for (i = 0; i < width; i++) {
printf("-");
}
printf(" ");
int j = 0, numWords = 0;
for (j = 0; j < strlen(input); j++) {
if (input[j] == ' ') {
numWords++;
}
}
numWords++;
double numLetters, avgWordLen;
if (strcmp(autoC, "yes") == 0) {
for (int n = 0; n < strlen(input); n++) {
if (input[n] == '3') {
input[n] = 'e';
}
if (input[n] == '1') {
input[n] = 'l';
}
if (input[n] == '0') {
input[n] = 'o';
}
if (input[n] == '7') {
input[n] = 't';
}
}
input[0] = toupper(input[0]);
if(input[strlen(input)] != '.'){
input[strlen(input)] = '.';
}
numLetters = strlen(input) - numWords;
avgWordLen = numLetters / numWords;
}
else{
numLetters = strlen(input) - numWords + 1;
avgWordLen = numLetters / numWords;
}
int k = 0;
if (strcmp(align, "left") == 0) {
printf("%s ", input);
} else if (strcmp(align, "center") == 0) {
if (width % 2 == 1) {
printf(" ");
}
for (k = 0; k < (width / 2) - (strlen(input) / 2); k++) {
printf(" ");
}
printf("%s ", input);
} else if (strcmp(align, "right") == 0) {
for (k = 0; k < width - strlen(input); k++) {
printf(" ");
}
printf("%s ", input);
}
printf("Words: %d ", numWords);
printf("Avg word length: %lf ", avgWordLen);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
