In C character specified R You are to write a program that w

In C:
character specified: R
You are to write a program that will compare characters the user enters to a known character
Your code should first prompt for the number of values the user will enter, then loop to allow the user to enter the characters. After each character
is entered your code should print out whether the character comes before, after, or is, the known character specified .
Your code should also keep track of how many total characters entered came before the known character, how many characters entered came after the known character,
and how many times the known character was entered.
In the comments of the program, list at least 2 test cases and the behavior of your program for these test cases.


Here is an example program execution with the known character being m:
Joe Sample,Weds 8am, 123456789
Please enter the number of characters you would like to process: 5
a
a comes before m
H
H comes before m
z
z comes after m
l
l comes before m
m
saw the known character
3 characters came before m, 1 characters came after m, m was seen 1 times.

Solution

Ans.

#include <stdio.h>

int main(void)
{
printf(\"Please enter the number of characters you would like to process:\ \");

   int n, before = 0, after = 0, same = 0;
   scanf(\"%d\", &n);
   char known = \'m\', ch;
   while(n--)
   {
       scanf(\"%c\", &ch);
       if ((int)known < (int)ch)
       {
           after++;
           printf(\"%c comes after %c\ \", ch, known);
       }
       else if ((int)known > (int)ch)
       {
           before++;
           printf(\"%c comes before %c\ \", ch, known);
       }
       else
       {
           same++;
           printf(\"saw the known character\ \");
          
       }
          
   }
   printf(\"%d characters came before %c, %d characters came after %c, %c was seen %d times.\ \", before, known, after, known, known, same);
   return 0;
}

In C: character specified: R You are to write a program that will compare characters the user enters to a known character Your code should first prompt for the
In C: character specified: R You are to write a program that will compare characters the user enters to a known character Your code should first prompt for the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site