So I have this code (in C++) and I'm not sure what I'm getting wrong with the actual code. I need to make it so the letter that the code outputs DOES NOT count characters and that the code chooses the letter that is earliest in the alphabet. (I added some examples at the bottom)    For this problem you must create a program that accepts a line of English text as input. The string may contain spaces, so make sure you read the entire line of input. Next your program should count the occurrences of each letter of the English alphabet and determine which letter occurs most frequently in the string. Print this letter to the screen. If two letters tie for the most frequent count, break the tie by choosing the letter that occurs first in the alphabet. For example if the input string was "aabb", then your program should identify 'a' as the most frequently occurring letter. Finally, remove all occurrences of this most frequently occurring letter by replacing them with the '-' character, and print the final string to the screen. HINT: Since ascii characters are stored as 8 bit ints "under the hood", and they are also represented in order (i.e. 'a' comes before 'b'); we can use them both as characters as well as for counting. Thus if we wanted to loop through all of the lowercase letters of the alphabet we could do something like this: for(char currentLetter = 'a'; currentLetter <= 'z'; currentLetter++){ //interesting code goes here... } Sample Input aaaab Sample Output a ----b Simplifying Assumptions You may assume that all letters are lowercase You only need to count the letters of the alphabet (i.e. don't worry about counting special characters like punctuation, digits, etc.) If two letters tie for the most frequent count, break the tie by choosing the letter that occurs first in the alphabet.   MY CODE #include <iostream>#include <string>#include <vector>#include <cctype>#include<bits/stdc++.h>using namespace std; string replaceLetter(string originalString, char character){ cout << character << endl;int originalLength = originalString.length(); for (int i = 0; i < originalLength; i++){ if (originalString[i] == character){ originalString[i] = '-'; } } return originalString;} int main(){int arr[26] = { 0 }; string s; getline(cin, s); string s1=s; //sort(s.begin(),s.end()); int originalLength = s.length(); int i = 0; int max_count = 0; int temp = 0; for ( i = 0; i < originalLength; i++){ temp = tolower(s[i]) - 97; arr[temp] ++; if (arr[temp] > max_count){ max_count = arr[temp]; } } vector<int> lettersWithMaxCount; for (i = 0; i < 26; i++){ if (arr[i] == max_count){ lettersWithMaxCount.push_back(i + 97);} } if (lettersWithMaxCount.size() == 1){ s = replaceLetter(s, lettersWithMaxCount[0]); } else{for (i = 0; i < originalLength; i++){ temp = s[i]; bool found = false; int result; for (std::vector<int>::iterator it = lettersWithMaxCount.begin();it != lettersWithMaxCount.end(); ++it){ if (*it == temp){ result = temp; found = true;break; } } if (found){ s = replaceLetter(s1, temp); cout << s << endl;return 0; } } } cout << s << endl; return 0;}   EXAMPLE OUTPUT Output differs. Input : This is a sentence.   Your output: s Thi- i- a -entence.   Expected output: e This is a s-nt-nc-.   EXAMPLE 2: Input: this???   Your output: this???   Expected output: h t-is???   Thanks!

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter2: Using Data
Section: Chapter Questions
Problem 4E: In this chapter, you learned that although a double and a decimal both hold floating-point numbers,...
icon
Related questions
icon
Concept explainers
Question

So I have this code (in C++) and I'm not sure what I'm getting wrong with the actual code. I need to make it so the letter that the code outputs DOES NOT count characters and that the code chooses the letter that is earliest in the alphabet. (I added some examples at the bottom) 

 

For this problem you must create a program that accepts a line of English text as input. The string may contain spaces, so make sure you read the entire line of input. Next your program should count the occurrences of each letter of the English alphabet and determine which letter occurs most frequently in the string. Print this letter to the screen. If two letters tie for the most frequent count, break the tie by choosing the letter that occurs first in the alphabet. For example if the input string was "aabb", then your program should identify 'a' as the most frequently occurring letter.

Finally, remove all occurrences of this most frequently occurring letter by replacing them with the '-' character, and print the final string to the screen.

HINT:

Since ascii characters are stored as 8 bit ints "under the hood", and they are also represented in order (i.e. 'a' comes before 'b'); we can use them both as characters as well as for counting. Thus if we wanted to loop through all of the lowercase letters of the alphabet we could do something like this:

for(char currentLetter = 'a'; currentLetter <= 'z'; currentLetter++){ //interesting code goes here... }

Sample Input

aaaab

Sample Output

a ----b

Simplifying Assumptions

  • You may assume that all letters are lowercase
  • You only need to count the letters of the alphabet (i.e. don't worry about counting special characters like punctuation, digits, etc.)
  • If two letters tie for the most frequent count, break the tie by choosing the letter that occurs first in the alphabet.

 

MY CODE

#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include<bits/stdc++.h>
using namespace std;

string replaceLetter(string originalString, char character){

cout << character << endl;
int originalLength = originalString.length();

for (int i = 0; i < originalLength; i++){
if (originalString[i] == character){
originalString[i] = '-';
}
}
return originalString;
}


int main(){
int arr[26] = { 0 };
string s;

getline(cin, s);

string s1=s;

//sort(s.begin(),s.end());

int originalLength = s.length();
int i = 0;
int max_count = 0;
int temp = 0;

for ( i = 0; i < originalLength; i++){
temp = tolower(s[i]) - 97;
arr[temp] ++;

if (arr[temp] > max_count){
max_count = arr[temp];
}
}

vector<int> lettersWithMaxCount;

for (i = 0; i < 26; i++){

if (arr[i] == max_count){

lettersWithMaxCount.push_back(i + 97);
}
}

if (lettersWithMaxCount.size() == 1){

s = replaceLetter(s, lettersWithMaxCount[0]);
}

else{
for (i = 0; i < originalLength; i++){
temp = s[i];
bool found = false;
int result;

for (std::vector<int>::iterator it = lettersWithMaxCount.begin();
it != lettersWithMaxCount.end(); ++it){

if (*it == temp){
result = temp;
found = true;
break;
}
}
if (found){
s = replaceLetter(s1, temp);
cout << s << endl;
return 0;
}
}
}
cout << s << endl;
return 0;
}

 

EXAMPLE OUTPUT

Output differs.
Input :
This is a sentence.
 
Your output:
s
Thi- i- a -entence.
 
Expected output:
e
This is a s-nt-nc-.
 
EXAMPLE 2:
Input:
this???
 
Your output:
this???
 
Expected output:
h t-is???
 
Thanks!
Expert Solution
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,