122k views
2 votes
Write a program which takes a string input, converts it to lowercase, then prints

the same string without the five most common letters in the English alphabet (e, t, a, i o).

1 Answer

2 votes

Answer:

Seeing as i don't know what language you want this made in, so I'll do it in two languages, Py and C++.

Here is a solution in C++:

#include <iostream>

#include <string>

#include <map>

int main() {

// prompt the user for a string

std::cout << "Enter a string: ";

std::string input;

std::getline(std::cin, input);

// convert the input string to lowercase

std::transform(input.begin(), input.end(), input.begin(), ::tolower);

// create a map to keep track of the frequency of each letter in the string

std::map<char, int> letter_counts;

for (const char& c : input) {

letter_counts[c]++;

}

// create a string of the five most common letters in the English alphabet

// (e, t, a, i, o)

std::string most_common_letters = "etai";

// remove the five most common letters from the input string

for (const char& c : most_common_letters) {

input.erase(std::remove(input.begin(), input.end(), c), input.end());

}

// print the resulting string

std::cout << "Resulting string: " << input << std::endl;

return 0;

}


Here is a solution in Python:

import string

# prompt the user for a string

input_str = input("Enter a string: ")

# convert the input string to lowercase

input_str = input_str.lower()

# create a string of the five most common letters in the English alphabet

# (e, t, a, i, o)

most_common_letters = "etai"

# remove the five most common letters from the input string

for c in most_common_letters:

input_str = input_str.replace(c, "")

# print the resulting string

print("Resulting string: ", input_str)

Explanation: Hope this helped

User Russell Strauss
by
5.0k points