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 the largest word in a given string.
Method 1
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string str, word, largestWord;
int maxLength = 0;
cout << "Enter any sequence of words or line: ";
getline(cin, str);
//Break a string into words
stringstream strStream(str);
while (strStream >> word) {
if (word.length() > maxLength) {
maxLength = word.length();
largestWord = word;
}
}
//Print the output
cout << "Largest word: " << largestWord << endl;
return 0;
}
Output
Enter any sequence of words or line: CodeDesire is about programming and technology
Largest word: programming
Method 2
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string SearchLargestWord(const string& input) {
//Break a string into words
stringstream strStream(input);
string word, largestWord;
size_t maxLength = 0;
while (strStream >> word) {
if (word.length() > maxLength) {
maxLength = word.length();
largestWord = word;
}
}
return largestWord;
}
int main() {
string input;
cout << "Enter any sequence of words or line: ";
getline(cin, input);
string largestWord = SearchLargestWord(input);
//Print the output
if (!largestWord.empty()) {
cout << "Largest word: " << largestWord <<endl;
} else {
cout << "No words found " <<endl;
}
return 0;
}
Output
Enter any sequence of words or line: CodDesire is about programming and technology
Largest word: programming