C++ Programming
- Write a program to print Hello, World on the standard output.
- Write a program that uses the multiplication operator, *, to print the product.
- Program to use
a separate statement to print each operand. - Write a program that uses a while to sum the numbers from 50 to 100.
- write a while that prints the numbers from ten down to zero.
- Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.
- What are the differences between int, long, long long, and short?
- To calculate a mortgage payment, what types would you use for the rate, principal, and payment? Explain why you selected each type.
- Determine the type of each of the following literals. Explain the differences among the literals in each of the four examples:
- What, if any, are the differences between the following definitions: int month = 9, day = 7; int month = 09, day = 07;
- What values do these literals represent? What type does each have? (a) “Who goes with F\145rgus?\012” (b) 3.14e1L (c) 1024f (d) 3.14L
- Using escape sequences, write a program to print 2M followed by a newline. Modify the program to print 2, then a tab, then an M, followed by a newline.
- Explain the following definitions. For those that are illegal, explain what’s wrong and how to correct it.
(a) std::cin >> int input_value;
(b) int i = { 3.14 };
(c) double salary = wage = 9999.99;
(d) int i = 3.14; - Write a program to calculate the highest two numbers in an array with length n, and print these numbers with their indexes.
- Write a C++ program to change every letter in a given string with the letter following it in the alphabet (i.e. a becomes b, p becomes q, z becomes a).
- Write a C++ program to find the largest word in a given string.
- Write a C++ program to count all the characters, blank spaces and words in a given string.
- Write a C++ program to find a word in a given string that has the highest number repeated letters.
Write a C++ program to find a word in a given string that has the highest number of repeated letters.
Method 1
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
string WordWithMostRepeatedLetters(const string& input)
{
string result = "";
int maxRepeatedCount = 0;
// Split the sentence into words
vector<string> words;
string currentWord;
for (char c : input)
{
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
{
if (!currentWord.empty())
{
words.push_back(currentWord);
currentWord.clear();
}
} else
{
currentWord += c;
}
}
if (!currentWord.empty())
{
words.push_back(currentWord);
}
// Iterate through each word to count the number of letters
for (const string& word : words)
{
unordered_map<char, int> letterCount;
int currentMaxRepeatedCount = 0;
for (char c : word)
{
letterCount++;
currentMaxRepeatedCount = max(currentMaxRepeatedCount, letterCount);
}
if (currentMaxRepeatedCount > maxRepeatedCount)
{
maxRepeatedCount = currentMaxRepeatedCount;
result = word;
}
}
return result;
}
int main() {
string input;
cout<<"Write any sentence for finding the most repeatd letters ";
getline(cin,input);
string MostRepeatedLettersInWord = WordWithMostRepeatedLetters(input);
cout <<"The word with the most repeated letters is: " <<MostRepeatedLettersInWord<<endl;
return 0;
}
Output
Write any sentence for finding the most repeatd letters Programming is engazzzing and funnnnn
The word with the most repeated letters is: funnnnn Method 2
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <string>
#include <algorithm>
using namespace std;
//Function to count repeated letters
int countRepeatedLetters(const string& word)
{
unordered_map<char, int> letterCount;
int maxRepeats = 0;
for (char ch : word)
{
letterCount[ch]++;
maxRepeats = max(maxRepeats, letterCount[ch]);
}
return maxRepeats;
}
//Function to find the word with highest number of repeated letters
string WordWithHighestRepeats(const string& input) {
istringstream iss(input);
string word, maxWord;
int maxRepeats = 0;
while (iss >> word)
{
int repeats = countRepeatedLetters(word);
if (repeats > maxRepeats)
{
maxRepeats = repeats;
maxWord = word;
}
}
return maxWord;
}
int main() {
string input;
cout << "Write any sentence for finding the most repeatd letters: ";
getline(cin, input);
string result = WordWithHighestRepeats(input);
if (!result.empty())
{
cout <<"The word with the most repeated letters is: "<<result<<endl;
} else
{
cout <<"There is no word with repeated letters";
}
return 0;
}
Output
Write any sentence for finding the most repeatd letters: Programming is engazzzing and funnnn
The word with the most repeated letters is: funnnn