Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program declares an array of chars of size maxSize, and prompts the user to

ID: 3806111 • Letter: T

Question

This program declares an array of chars of size maxSize, and prompts the user to type letters, either until the array is full or until the user types the character #. After next asking the user to input a letter, it outputs the number of times that letter occurs in the input sequence. The program allows the user to repeat this as often as she or he likes. Thus, if maxSize is 10, and the user types abcb*. and then selects b as the letter to count, the program outputs The letter b occurs 2 times in your input sequence. If the user selected z as the letter to count, the program outputs The letter z occurs 0 times in your input sequence If the user types precisely maxSize letters - say abcdeabcde - then * is not needed to terminate the input After each run. the user is asked whether a repeat computation is w anted. You should explore what happens when the user types in more than maxSize letters before hitting return, perhaps, setting your program up to handle this 'overflow as graciously as you can.

Explanation / Answer


// C code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>


int main()
{
int maxSize = 50;
  
char letter;
char repeat;

while(1)
{
char charArray[maxSize];
int count = 0;
printf(" Enter letters(# to end): ");
// even if more than maxsize elements are entered,
// array with take only first maxsize characters
scanf("%s",charArray);

  
printf("Enter a letter: ");
scanf(" %c",&letter);
for (int i = 0; i < sizeof(charArray)/sizeof(charArray[0]); ++i)
{
if(charArray[i] == letter)
count++;
}

printf("The letter %c occurs %d times in your input sequence ",letter,count);

printf("Enter y to repeat, n to exit: ");
scanf(" %c",&repeat);

if(repeat == 'n')
break;
}
return 0;
}


/*
output:

Enter letters(# to end): jamesande#
Enter a letter: a
The letter a occurs 2 times in your input sequence
Enter y to repeat, n to exit: y

Enter letters(# to end): darrens#
Enter a letter: r
The letter r occurs 2 times in your input sequence
Enter y to repeat, n to exit: y

Enter letters(# to end): kylemonigue#
Enter a letter: a
The letter a occurs 0 times in your input sequence
Enter y to repeat, n to exit: n


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote