Program for length of the longest word in a sentence
Last Updated :
15 Sep, 2024
Given a string, we have to find the longest word in the input string and then calculate the number of characters in this word.
Examples:
Input : A computer science portal for geeks
Output : Longest word's length = 8
Input : I am an intern at geeksforgeeks
Output : Longest word's length = 13
The idea is simple, we traverse the given string. If we find end of word, we compare length of ended word with result. Else, we increment length of current word.
C++
// C++ program to find the number of
// charters in the longest word in
// the sentence.
#include <iostream>
using namespace std;
int LongestWordLength(string str)
{
int n = str.length();
int res = 0, curr_len = 0, i;
for (int i = 0; i < n; i++) {
// If current character is
// not end of current word.
if (str[i] != ' ')
curr_len++;
// If end of word is found
else {
res = max(res, curr_len);
curr_len = 0;
}
}
// We do max one more time to
// consider last word as there
// won't be any space after
// last word.
return max(res, curr_len);
}
// Driver function
int main()
{
string s =
"I am an intern at geeksforgeeks";
cout << LongestWordLength(s);
return 0;
}
Java
// Java program to find the number of charters
// in the longest word in the sentence.
import java.util.*;
class LongestWordLength
{
static int LongestWordLength(String str)
{
int n = str.length();
int res = 0, curr_len = 0;
for (int i = 0; i < n; i++)
{
// If current character is not
// end of current word.
if (str.charAt(i) != ' ')
curr_len++;
// If end of word is found
else
{
res = Math.max(res, curr_len);
curr_len = 0;
}
}
// We do max one more time to consider
// last word as there won't be any space
// after last word.
return Math.max(res, curr_len);
}
public static void main(String[] args)
{
String s = "I am an intern at geeksforgeeks";
System.out.println(LongestWordLength(s));
}
}
Python
# Python3 program to find the
# number of charters in the
# longest word in the sentence.
def LongestWordLength(str):
n = len(str)
res = 0; curr_len = 0
for i in range(0, n):
# If current character is
# not end of current word.
if (str[i] != ' '):
curr_len += 1
# If end of word is found
else:
res = max(res, curr_len)
curr_len = 0
# We do max one more time to consider
# last word as there won't be any space
# after last word.
return max(res, curr_len)
# Driver Code
s = "I am an intern at geeksforgeeks"
print(LongestWordLength(s))
# This code is contribute by Smitha Dinesh Semwal.
C#
// C# program to find the number of charters
// in the longest word in the sentence.
using System;
class GFG {
static int LongestWordLength(string str)
{
int n = str.Length;
int res = 0, curr_len = 0;
for (int i = 0; i < n; i++)
{
// If current character is not
// end of current word.
if (str[i] != ' ')
curr_len++;
// If end of word is found
else
{
res = Math.Max(res, curr_len);
curr_len = 0;
}
}
// We do max one more time to consider
// last word as there won't be any space
// after last word.
return Math.Max(res, curr_len);
}
public static void Main()
{
string s = "I am an intern at geeksforgeeks";
Console.Write(LongestWordLength(s));
}
}
// This code is contributed by nitin mittal.
JavaScript
function longestWordLen(s) {
let n = s.length;
let res = 0, currLen = 0;
for (let i = 0; i < n; i++) {
// If current character is not a space,
// it's part of the current word.
if (s[i] !== ' ') {
currLen++;
} else {
// End of word, update the result
// and reset current length.
res = Math.max(res, currLen);
currLen = 0;
}
}
// Check last word since there's no space after it.
return Math.max(res, currLen);
}
// Driver function
let s = "I am an intern at geeksforgeeks";
console.log(longestWordLen(s));
PHP
<?php
// PHP program to find the
// number of charters in
// the longest word in the
// sentence.
function LongestWordLength($str)
{
$n = strlen($str);
$res = 0; $curr_len = 0;
for ($i = 0; $i < $n; $i++)
{
// If current character is
// not end of current word.
if ($str[$i] != ' ')
$curr_len++;
// If end of word is found
else
{
$res = max($res, $curr_len);
$curr_len = 0;
}
}
// We do max one more
// time to consider last
// word as there won't
// be any space after
// last word.
return max($res, $curr_len);
}
// Driver Code
$s = "I am an intern at geeksforgeeks";
echo (LongestWordLength($s));
// This code is contributed by
// Manish Shaw(manishshaw1)
?>
Another Approach:
C++14
// C++ program to find the number of charters
// in the longest word in the sentence.
#include<bits/stdc++.h>
using namespace std;
int LongestWordLength(string str)
{
int counter = 0;
string words[100];
for (short i = 0; i < str.length(); i++)
{
if (str[i] == ' ')
counter++;
else
words[counter] += str[i];
}
int length = 0;
for(string word:words)
{
if(length < word.length())
{
length = word.length();
}
}
return length;
}
// Driver code
int main()
{
string str = "I am an intern at geeksforgeeks";
cout << (LongestWordLength(str));
}
// This code contributed by Rajput-Ji
Java
// Java program to find the number of charters
// in the longest word in the sentence.
class GFG {
static int LongestWordLength(String str)
{
String[] words = str.split(" ");
int length = 0;
for(String word:words){
if(length < word.length()){
length = word.length();
}
}
return length;
}
// Driver code
public static void main(String args[])
{
String str = "I am an intern at geeksforgeeks";
System.out.println(LongestWordLength(str));
}
}
Python
# Python program to find the number of characters
# in the longest word in the sentence.
def longestWordLength(string):
length = 0
# Finding longest word in sentence
for word in string.split():
if(len(word) > length):
length = len(word)
return length
# Driver Code
string = "I am an intern at geeksforgeeks"
print(longestWordLength(string))
# This code is contributed by Vivekkumar Singh
C#
// C# program to find the
// number of charters in
// the longest word in
// the sentence.
using System;
class GFG
{
static int LongestWordLength(string str)
{
String[] words = str.Split(' ');
int length = 0;
for(int i = 0; i < words.Length; i++)
{
if(length < words[i].Length)
{
length = words[i].Length;
}
}
return length;
}
// Driver code
static void Main()
{
string str = "I am an intern at geeksforgeeks";
Console.Write(LongestWordLength(str));
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
JavaScript
// JavaScript program to find the number of characters
// in the longest word in the sentence.
function longestWordLength(string)
{
let length = 0;
const words = string.split(" ");
// Finding longest word in sentence
for (const word of words) {
if (word.length > length) {
length = word.length;
}
}
return length;
}
const sentence = "I am an intern at geeksforgeeks";
console.log(longestWordLength(sentence));
Similar Reads
Print Words with Prime length from a Sentence + Given a string S, the task is to print all words with prime length in the given string. Examples: Input: S = "This is a python programming language"Output: isprogrammingExplanation: Length of is is 2 and length of programming is 11 both are primes Input: S = "You are using geeksforgeeks"Output: Yo
15+ min read
Print longest palindrome word in a sentence Given a string str, the task is to print longest palindrome word present in the string str.Examples: Input : Madam Arora teaches Malayalam Output: Malayalam Explanation: The string contains three palindrome words (i.e., Madam, Arora, Malayalam) but the length of Malayalam is greater than the other t
14 min read
Program to find Smallest and Largest Word in a String Given a string, find the minimum and the maximum length words in it. Examples: Input : "This is a test string"Output : Minimum length word: a Maximum length word: stringInput : "GeeksforGeeks A computer Science portal for Geeks"Output : Minimum length word: A Maximum length word: GeeksforGeeksMethod
15+ min read
Javascript Program To Find Length Of The Longest Substring Without Repeating Characters Given a string str, find the length of the longest substring without repeating characters. For âABDEFGABEFâ, the longest substring are âBDEFGAâ and "DEFGAB", with length 6.For âBBBBâ the longest substring is âBâ, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below
5 min read
Length of the longest substring that do not contain any palindrome Given a string of lowercase, find the length of the longest substring that does not contain any palindrome as a substring. Examples: Input : str = "daiict" Output : 3 dai, ict are longest substring that do not contain any palindrome as substring Input : str = "a" Output : 0 a is itself a palindrome
6 min read