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

**C++ only, standard library. We are supposed to create a tower of hanoi program and do...
**C++ only, standard library. We are supposed to create a tower of hanoi program and do a sequence of 100 disks. We are supposed to run the program and then answer two questions: 1) How long does your computer take to run(time)? 2) If I could move 1 disk per second how long would it take? Here is my program so far: void towerOfHanoi(int numDisks, char from_rod, char to_rod, char aux_rod) { int count=0; if (numDisks == 1) { //cout...
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 program in c language to count the number of spaces ina sentence .the sentence...
write a program in c language to count the number of spaces ina sentence .the sentence is saved in the string 'sentence' write it in a repl.it and along with input,output and algorithm
MIPS Program I'm trying to write a program that will take in two numbers from the...
MIPS Program I'm trying to write a program that will take in two numbers from the user and output the sum at the end. However, I keep getting very wrong answers. For example, I add 2 and 2 and get 268501000. Help would be appreciated. .data #starts data use userIn1:    .word 4 #sets aside space for input    userIn2:    .word 4 #sets aside space for input total:    .word 4 #sets space aside for input    request:   ...
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...
Hi, i'm creating a c++ program with a linked list of Employees, i'm running into issues...
Hi, i'm creating a c++ program with a linked list of Employees, i'm running into issues as far as displaying programmers only portion and the average salary of all the employees. Please make any changes or comments ! Employee.h file: #ifndef EMPLOYEE_H #define EMPLOYEE_H #include using namespace std; enum Role {programmer, manager, director}; struct Employee { std::string firstName; std::string lastName; int SSN; std::string department; double salary; Role role; }; #endif #ifndef NODE_H #define NODE_H Node.h file: #include "Employee.h" typedef Employee...
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...
Create a program in Visual studio c++ Frank wants to take his wife, son, and daughter...
Create a program in Visual studio c++ Frank wants to take his wife, son, and daughter on a vacation this next summer. Create a program named "Your last name_Vacation" This program will prompt Frank to choose a number between 1 and 6. Use a switch…case to determine which number he chose. Use a Do…while loop to return to the prompt if the number is <1 and >6. 1 - Hawaii – 7 days / 6 nights 2 - New York...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT