Program for length of the longest word in a sentence
Last Updated :
27 Jan, 2023
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.
Implementation:
C++
#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 (str[i] != ' ' )
curr_len++;
else {
res = max(res, curr_len);
curr_len = 0;
}
}
return max(res, curr_len);
}
int main()
{
string s =
"I am an intern at geeksforgeeks" ;
cout << LongestWordLength(s);
return 0;
}
|
Java
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 (str.charAt(i) != ' ' )
curr_len++;
else
{
res = Math.max(res, curr_len);
curr_len = 0 ;
}
}
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));
}
}
|
Python3
def LongestWordLength( str ):
n = len ( str )
res = 0 ; curr_len = 0
for i in range ( 0 , n):
if ( str [i] ! = ' ' ):
curr_len + = 1
else :
res = max (res, curr_len)
curr_len = 0
return max (res, curr_len)
s = "I am an intern at geeksforgeeks"
print (LongestWordLength(s))
|
C#
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 (str[i] != ' ' )
curr_len++;
else
{
res = Math.Max(res, curr_len);
curr_len = 0;
}
}
return Math.Max(res, curr_len);
}
public static void Main()
{
string s = "I am an intern at geeksforgeeks" ;
Console.Write(LongestWordLength(s));
}
}
|
PHP
<?php
function LongestWordLength( $str )
{
$n = strlen ( $str );
$res = 0; $curr_len = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $str [ $i ] != ' ' )
$curr_len ++;
else
{
$res = max( $res , $curr_len );
$curr_len = 0;
}
}
return max( $res , $curr_len );
}
$s = "I am an intern at geeksforgeeks" ;
echo (LongestWordLength( $s ));
?>
|
Javascript
<script>
function LongestWordLength(str)
{
var n = str.length;
var res = 0,
curr_len = 0;
for ( var i = 0; i < n; i++)
{
if (str[i] !== " " ) curr_len++;
else
{
res = Math.max(res, curr_len);
curr_len = 0;
}
}
return Math.max(res, curr_len);
}
var s = "I am an intern at geeksforgeeks" ;
document.write(LongestWordLength(s));
</script>
|
Another Approach:
C++14
#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;
}
int main()
{
string str = "I am an intern at geeksforgeeks" ;
cout << (LongestWordLength(str));
}
|
Java
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;
}
public static void main(String args[])
{
String str = "I am an intern at geeksforgeeks" ;
System.out.println(LongestWordLength(str));
}
}
|
Python3
def longestWordLength(string):
length = 0
for word in string.split():
if ( len (word) > length):
length = len (word)
return length
string = "I am an intern at geeksforgeeks"
print (longestWordLength(string))
|
C#
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;
}
static void Main()
{
string str = "I am an intern at geeksforgeeks" ;
Console.Write(LongestWordLength(str));
}
}
|
Javascript
function longestWordLength(string)
{
let length = 0;
const words = string.split( " " );
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));
|
Share your thoughts in the comments
Please Login to comment...