C++ Programming
- Write a C++ Program to print a welcome text in a separate line.
- Write a Program in C++ to add two numbers.
- C++ program for addition of two numbers using functions.
- Add two numbers in C++ using class.
- Addition of two numbers using call by reference in C++
- C++ program to write a program to add two numbers using call by value.
- Swapping of two numbers in C++ without using third variable.
- Swap two numbers in C++ using function.
- C++ program to swap two numbers using class.
- C++ program to swap two numbers using call by value.
- C++ program to swap two numbers using pointers.
- Write a program to swap private data members of two classes using friend function.
- write a program to add data objects of two different classes using friend function.
- Threading in C++.
- C++ thread lambda
- C++ thread lambda with parameter
- C++ thread lambda pass by reference
- Singleton C++ example
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