C ONLY PLEASE Write a program that prompts the user to enter a string and prints
ID: 3600519 • Letter: C
Question
C ONLY PLEASE
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.
Sample Input:
Enter a string with no spaces: 0000011111
Output:
Goodness of the input string is 5
Sample Input:
Enter a string with no spaces: 111110000
Output:
Goodness of the input string is 5
Sample Input:
Enter a string with no spaces: nn11
Output:
Goodness of the input string is 0
Sample Input:
Enter a string with no spaces: 00xyz111
Output:
Goodness of the input string is 0
Explanation / Answer
#include <stdio.h>
int main()
{
char s[50];
int i, count = 0;
printf("Enter a string with no spaces: ");
scanf("%s", &s);
for(i=0;s[i]!='';i++) {
if(s[i]=='0' || s[i] == '1') {
if(s[i] == '1') count++;
} else {
count = 0;
break;
}
}
printf("Goodness of the input string is: %d ", count);
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.