Question

In: Computer Science

I'm supposed to create a c++ program which is supposed to take a sentence as an...

I'm supposed to create a c++ program which is supposed to take a sentence as an input and then output a sorted list of the occurrence of each letter.

ex.

Enter a phrase: It's a hard knock life

A2

I2
K2

C1

D1

E1

F1

H1

L1

N1

O1

R1

S1

T1

I was also given a recommended code to use as a bubble sort

procedure bubbleSort( A : list of sortable items )

n = length(A)
repeat

swapped = false

for i = 1 to n-1 inclusive do

/* if this pair is out of order */ if A[i-1] > A[i] then

/* swap them and remember something changed */ swap( A[i-1], A[i] )
swapped = true

end if end for

until not swapped

end procedure

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;

void bubbleSort(string &str)
{
        int n = str.length();
        for (int i = 0; i < n - 1; i++)
                for (int j = 0; j < n - i - 1; j++)
                        if (str[j] > str[j + 1])
                        {
                                // swap str[j+1] and str[j]
                                int temp = str[j];
                                str[j] = str[j + 1];
                                str[j + 1] = temp;
                        }
}

int main() {



        string s;

        cout<<"Enter a phrase: ";

        getline(cin, s);

        string str;

        for (int i = 0; i < s.length(); ++i) {
                if ((s[i] <= 'z' && s[i] >= 'a') || (s[i] <= 'Z' && s[i] >= 'A')) {
                        str += toupper(s[i]);
                }
        }
        bubbleSort(str);


        for (int i = 0; i < str.length(); ++i) {
                cout << str[i];
                int cnt = 0;

                if(i == str.length()-1){
                        cout<<1<<endl;
                        break;
                }

                while (str[i] == str[i + 1]) {
                        i++;
                        cnt++;
                }
                cout<<cnt+1<<endl;
        }

        return 0;

}

OUTPUT:


Related Solutions

Need to create a program in C++ that can display/write into a file called marks.txt. I'm...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm not too worried about the functions, but I don't know how to store the different marks into a arrays. Any help would be appreaciated. Here's some details about the assignment. Student marks are kept in a text file as a single column. Each student may have a different number of assessments and therefore scores. The data recorded in the file for each student start...
Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
The c++ program is supposed to store the txt into a stack depending on the what...
The c++ program is supposed to store the txt into a stack depending on the what the random number generator assigns each student and stores them into the stack. But it keeps giving me the same outputs and I can't figure out why? #include<iostream> #include<fstream> #include<string> #include<sstream> #include<stack> using namespace std; struct student { int ID; string last_name; double gpa; }; void read_std_file(student* std); void store_std_into_stack(stack <student> ss, student std[]); void wite_output_file(student std); void read_std_file(student* std) { ifstream file; file.open("C://temp//students.txt");...
Your program should take a string representing a sentence in English and format it properly. The...
Your program should take a string representing a sentence in English and format it properly. The input sentence may have any or all of the following errors: Random letters may be capitalized. The sentence may not end with a proper punctuation mark (period, question mark, or exclamation point). There may be spaces at the beginning or end, or more than one space between words. Format the sentence to fit the following rules: The first letter of the first word should...
In 2 to 3 paragraphs describe the C program written below (What the program is supposed...
In 2 to 3 paragraphs describe the C program written below (What the program is supposed to do). State the requirements for the program (How does the program meet the requirements) and discuss the logical process your program uses to meet those requirements (The process steps to meet the requirements). #include "stdio.h" int main(void) { //initialize array int arr[100];   //initialize variables   int i=0, j=0, n=0;    //infinite loop which will stop when user enters -1   while(n != -1) {   ...
Description: To create a Visual Basic program that will obtain a sentence from the user, parse...
Description: To create a Visual Basic program that will obtain a sentence from the user, parse the sentence, and then display a sorted list of the words in the sentence with duplicate words removed. Tasks: Design a method (algorithm) to solve the above problem using pseudocode or a flowchart. Translate your algorithm into an appropriate VB program and test it using the following input sentence. "We are the world We are the children" Submit: Pseudocode version of algorithm/Flowchart version of...
5. Take user input and give corresponding output. User will enter a sentence. The program will...
5. Take user input and give corresponding output. User will enter a sentence. The program will output the word that appears most frequently in this sentence. If there are multiple words with same frequency, output the first of these words. Please enter a sentence: I like batman because batman saved the city many times. The most frequent word is: batman The frequency is: 2 PYTHON
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Create a C++ program which will accept an unlimited number of scores and calculates the average...
Create a C++ program which will accept an unlimited number of scores and calculates the average score. You will also need to prompt for the total points possible. Assume each test is worth 100 points. Requirements. • Enter the Student ID first, then prompt for grades. Keep prompt- ing for grades until the client enters ’calc’. That triggers final pro- cessing. • Make your code as reliable as possible. • Make your program output easy to read. • You cannot...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT