
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program for length of the longest word in a sentence
Given with the sentence of multiple strings and the task is to find the length of the longest string in the sentence.
Example
Input-: hello I am here Output-: maximum length of a word is: 5 Input-: tutorials point is the best learning platform Output-: maximum length of a word is: 9
Approach used in the below program is as follows −
- Input the string in an array of strings
- Traverse the loop till the end of a sentence is not found
- Traverse one particular string of a sentence and calculate its length. Store the length in a variable
- Pass the integer values of length of strings stored in a temporary variable to a max() function that will return the maximum value from the given lengths of a string.
- Display the maximum length returned by a max() function
Algorithm
Start Step 1-> declare function to calculate longest word in a sentence int word_length(string str) set int len = str.length() set int temp = 0 set int newlen = 0 Loop For int i = 0 and i < len and i++ IF (str[i] != ' ') Increment newlen++ End Else Set temp = max(temp, newlen) Set newlen = 0 End return max(temp, newlen) step 2-> In main() declare string str = "tutorials point is the best learning platfrom" call word_length(str) Stop
Example
#include <iostream> using namespace std; //function to find longest word int word_length(string str) { int len = str.length(); int temp = 0; int newlen = 0; for (int i = 0; i < len; i++) { if (str[i] != ' ') newlen++; else { temp = max(temp, newlen); newlen = 0; } } return max(temp, newlen); } int main() { string str = "tutorials point is the best learning platfrom"; cout <<"maximum length of a word is : "<<word_length(str); return 0; }
Output
IF WE RUN THE ABOVE CODE IT WILL GENERATE FOLLOWING OUTPUT
maximum length of a word is : 9
- Related Articles
- Print longest palindrome word in a sentence in C Program
- Finding the length of second last word in a sentence in JavaScript
- Program to find length of longest diminishing word chain in Python?
- Program to find length longest prefix sequence of a word array in Python
- Write a C program to calculate the average word length of a sentence using while loop
- PHP program to find the first word of a sentence
- Python Program to return the Length of the Longest Word from the List of Words
- Java program to reverse each word in a sentence
- Python program to reverse each word in a sentence?
- Python Program to replace a word with asterisks in a sentence
- Java Program to replace a word with asterisks in a sentence
- Program to find length of longest word that can be formed from given letters in python
- Java program to count the characters in each word in a given sentence
- PHP program to replace a word with a different symbol in a sentence
- Python program to remove all duplicates word from a given sentence.

Advertisements