Question 15: Input a string and fill out frequency table of all alphabets. Print only nonzero entries for all alphabets. Uppercase and lowercase letters should be handled  separately.  Then  print  pictogram  (using  character  of  your  own  choice)  for  uppercase letters only.

February 13, 2019
Question 15: Input a string and fill out frequency table of all alphabets. Print only nonzero entries for all alphabets. Uppercase and lowercase letters should be handled  separately.  Then  print  pictogram  (using  character  of  your  own  choice)  for  uppercase letters only.
/*
Name:                   Mudassar Ali
Roll:                   BCSF15M045   [Morning]
Assignment:             No# 05
Teacher:                Miss Sadia Shahzad
*/
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout<<"\tThis program will display the frequency tables and Pictograph!!\n\n" ;

int count1 [26]={0};       // Counting number of occurrences of Upper_case letters.
int count2 [26]={0};       // Counting number of occurrences of lower_case letters.
char str[101];             // To store the string input.
str[101] = '\0';           // To restrict the user to enter 100 MAX Characters.
bool flag = false;         // Flag for the validation of string letter that are Alphabets.

cout<<"Enter the string:";
cin>>str;                  // Inputting the string.
int len = strlen(str);     // To calculate the length of String . It can also be calculated using loop.

for(int i=0; str[i]!='\0';i++)  // Loop to count the upper and lower case letters.
{
    if(!((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))) // Condition to validate that only Alphabets are in the string.
    {
        flag = true;
        break;
    }
    if(str[i]>='A'&& str[i]<='Z')   // Condition to Check Letter are capitals.
    {
         count1[str[i] - 'A']++;    // To count the Upper_case letters.
    }
    else if(str[i]>='a'&& str[i]<='z') // Condition to Check Letter are lower.
    {
        count2[str[i] - 'a']++;     // To count the Lower_case letters.
    }
}
   if(flag)                        // If flag becomes true  after validating the string. it shows string is invalid.
   cout<<"Enter String is an Invalid String!!";

   if(len>=1 && len<=100)          // If the length of the string is 100 then the Letters frequency tables will be printed.
    {
      char ch1 = 'A';              // Initialization of the Upper_case letters with there frequency.

      char ch2 = 'a';              // Initialization of the Upper_case letters with there frequency.

      cout<<"\nFrequency Table for Upper-case Letters:\n\n";

      for(int i = 0; i<26;i++,ch1++) // Loop to print the Upper_case letters with their frequency.
      {
          if(count1[i]>0)            // Condition to print the frequency Greater than Zero (0).
          cout<<ch1<<" ------> "<<count1[i]<<"\n\n";
      }

      cout<<"Frequency Table for Lower-case Letters:\n\n";

      for(int i = 0; i<26;i++,ch2++) // Loop to print the Lower_case letters with their frequency.
      {
          if(count2[i]>0)            // Condition to print the frequency Greater than Zero (0).
          cout<<ch2<<" ------> "<<count2[i]<<"\n\n";
      }
      cout<<"\nPictograph for String Characters:\n\n";  // Pictograph for only Upper_case letters.
      cout<<"  "<<(char)218<<endl;                                                  // Portion of code to print the pictograph.
      char ch = 'A';                 // Initialization of the Upper_case letters.

      for(int a=0; a<26;a++,ch++)    // Loop to iterate the set of statement till condition becomes false or desire output displayed.
      {
          if(count1[a]!=0)            // Condition to check the frequency Greater than Zero (0).
          {
              if(a>0)                    // To skip the below statement for the first iteration of loop.
              {
              cout<<"  "<<(char)179<<endl;      // To print the line in front of Upper_case letter on the output screen.
              }

              cout<<ch<<" "<<(char)179;         // To print the Upper_case letters and line.

              for(int b = 0; b<count1[a];b++)  // To display the frequency of the Upper_case letters.
              cout<<" "<<(char)178;            // To display the shapes for each upper letters.

              cout<<endl;                // To add the newline on the output screen.
          } // end of IF
      }     // end of for loop
    }       // end of IF
    cout<<"  "<<(char)192<<endl;
 return 0;
}

No comments:

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
Powered by Blogger.