Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why will my code return an error when the input is more than one word? #include #include #include #include int main(){ char input[200]; int width;

Why will my code return an error when the input is more than one word?

#include

#include

#include

#include

int main(){

char input[200];

int width;

char align[50];

char autoC[50];

printf("Input: ");

scanf("%s", input);

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 = 0;

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 + 1;

}

}

numWords = numWords + 1;

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]);

input[strlen(input)] = '.';

}

double avgWordLen = j / numWords, 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Public Finance Fundamentals

Authors: K. Moeti

3rd Edition

148512946X, 9781485129462

Students also viewed these Databases questions

Question

Explain the various techniques of Management Development.

Answered: 1 week ago