Question 4 (10 marks). Write a program that prompts the user to enter a string a
ID: 3603765 • Letter: Q
Question
Question 4 (10 marks). Write a program that prompts the user to enter a string and prints the goodness of the string. The goodness of a string in general, is calculated in the following way: If the string contains any letters except for 0 or 1, then its goodness is 0. Otherwise, its goodness is the number of 1's in the string Call your source code file Name the c file "lastnameFirstnameA2Q4.c". (Ritu's file would be called chaturvediRituA204.c) Sample Input: Enter a string with no spaces: 0000011111 Output: Goodness of the input string is 5 Sample Input: Output: Goodness of the input string is 5 Sample Input: Enter a string with no spaces: nn1l Output: Goodness of the input string is 0 Sample Input: Enter a string with no spaces: 00xyz111 output: Goodness of the input string is 0Explanation / Answer
Please find the well tested C program below.
#include <stdio.h>
int main()
{
printf("Hello, World! ");
char str[100];
int count = 0;
printf( "Enter a string with no spaces: ");
scanf("%s", str);
int i;
for(i = 0; i < sizeof(str); i++ ){
if(str[i] == '0' || str[i] == '1') {
if(str[i] == '1') {
count++;
}
} else {
break;
}
}
printf("Goodness of the string is %d ", count);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.