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

Using C++ 2.9% 2.9% 5.7% 2.9% 2.9% 2.9% 2.9% 2.9% 2.9% 11.4% 2.9% 2.9% 5.7% 2.9%

ID: 3603026 • Letter: U

Question

Using C++

2.9% 2.9% 5.7% 2.9% 2.9% 2.9% 2.9% 2.9% 2.9% 11.4% 2.9% 2.9% 5.7% 2.9% 5.7% 5.7% 2.9% 2.9% 2.9% 2.9% 2.9% Many string analysis tasks (particularly in genomics and cryptography) involve counting character occurrences. Give a string, we would like to find the probability of occurrence of each character, relative to the other alphabetic characters in the string. For example, each character in the string "supercalifragilisticexpialidocious has the following probability of occurrence: a: 8.8%, c: 8.8%, d: 2.9%, e: 5.9%, f: 2.9%, g: 2.9%, i: 28.6%, 1: 8.8%, o: 5.9%, p: 5.9%, r: 5.9%, s: 8.8%, t: 2.9%, u:5.9%, x: 2.9% j: 1: Write a C++program that accepts a string as input from a user and calculates the probability of occurrence (a percentage) for each alphabetic character in the word. Use an array to store the counts of each character n: Below are examples from three different runs of the program. O: Enter a string: supercalifragilisticexpialidocious r: 8.8% 8.8% 2.9% 5.9% 2.9% 2.9% 20.6% 8.8% 5.9% 5.9% 5.9% 8.8% 2.9% 5.9% 2.9% a: c: e: f: i: 1: o: p: r: s: t: u: Enter a string: First line (press Enter) Second line (press Enter) (press Enter, this is an empty 1ine) 5·3% 53% 15·8% 5.3% 15.8% 16. 5% 15.8% 5·3% 5.3% 16.5% Enter a string: The quick brown fox jumps over the lazy dog e: f: 1: a: b: c: d: 2.9% 2.9% 2.9% 2.9% 86% O: r: S:

Explanation / Answer

#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

int main(){

    string inp;
    cout << "Enter a string : ";
    getline(cin,inp);

    double n = (double)inp.size();
    char a = 'a';
    for (int i = 0; i<26; i++){
       double count = 0;
       for (int j = 0; j<n; j++){
          if (a == inp[j])
             count++;
       }
       if (count > 0){
          cout << a << ":" << " " << setprecision(1) << fixed << (count * 100)/n << "%" << endl;
       }
       a = a + 1;
    }
}

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