Programming in C Exercise 3: Write a complete program using two arrays, upper an
ID: 3762445 • Letter: P
Question
Programming in C Exercise 3:
Write a complete program using two arrays, upper and lower to keep the upper and lower case letters in the alphabet respectively.
Ask the user to enter string (example):
User Input: This is a test from Mars. Soon you will see who is from Mars!!! Maybe Dr. L?
Your program should parse the string and keep track of number of alphabet. Both arrays are indexed from 0 to 25. The logical way to do this is to use upper[0] to count the number of ‘A’, and upper[1] to count number of ‘B’ and so on. Likewise for the lower case array.
Your output should look like this:
A: 0 a: 2
B: 0 b: 1
….etc.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<string.h>
int main()
{
char upper[26], lower[26], input[255];
int i;
int countUpper=0;
int countLower=0;
for (i=0; i<26; i++) {
lower[i]=upper[i]=0;
}
printf("Enter a string: ");
fgets (input, 255, stdin);
int len = strlen(input);
for (i=0; i<len; i++) {
if (input[i]>='a' && input[i] <='z') {
lower['a'-input[i]]++;
}
if (input[i]>='A' && input[i] <='Z') {
upper['A'-input[i]]++;
}
}
for (i=0; i<26; i++) {
printf("%c:%d %c:%d ", 'A'+i, upper[i], 'a'+i, lower[i]);
}
printf(" ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.