Write a C function “counters”, that performs the following: • It gets a string f
ID: 3761996 • Letter: W
Question
Write a C function “counters”, that performs the following:
• It gets a string from the main program;
• Counts number of upper case letters in the string;
• Counts number of lower case letters in the string;
• Counts number of digits in the string;
• Counts number blanks in the string;
• Return all the 4 values to the main program through parameters using pointers to achieve call-by-reference effect.
Write the main function which calls the function “counters” to get a string, calculate the 4 values, and output shows in the display. The skeleton program is:(Use the skeleton program)
#include <stdio.h>
int getaline(char *s, int limit);
void counter(char *s, int *p);
int main(){
int limit=100;
char str[101];
int num;
printf("Please input a string:");
getaline(str,limit);
int a[4]={0};
counter(str, a);
printf("There are %d upper case letters, %d lower case letters, %d digits, %d blanks", a[0], a[1], a[2], a[3]);
return 0;
}
int getline(char *s, int limit) {
}
void counter(char *s, int *p){
}
Explanation / Answer
#include <stdio.h>
void getaline(char *s, int limit);
void counter(char *s, int *p);
int main(){
int limit=100;
char str[101];
int num;
printf("Please input a string:");
getaline(str,limit);
int a[4]={0};
counter(str, a);
printf("There are %d upper case letters, %d lower case letters, %d digits, %d blanks", a[0], a[1], a[2], a[3]);
return 0;
}
void getaline(char *s, int limit) {
int bytes_read = 1;
size_t bufsize = limit;
bytes_read = getline(&s, &bufsize, stdin);
if (bytes_read == -1)
{
puts ("ERROR!");
}
}
void counter(char *s, int *p){
int i = 0, upper = 0, lower = 0,nob=0,nod=0;
while (s[i] != '') {
if (s[i] >= 'A' && s[i] <= 'Z')
upper++;
if (s[i] >= 'a' && s[i] <= 'z')
lower++;
if (isdigit(s[i])) /* counting number of digits. */
nod++;
if (s[i] == ' ')
nob++;
i++;
}
p[0]=upper;
p[1]=lower;
p[2]=nod;
p[3]=nob;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.