TCS NQT Longest Word Coding Question With Solution

TCS NQT Longest Word Coding Question With Solution

TCS NQT Longest Word Coding Question With Solution

Problem statement

An English school teacher is teaching how to build sentences for kindergarten students. She starts by teaching two words in a sentence, then 3 words and so on. The next step is that she asks the students to find which word in the sentence they have made has the maximum number of alphabets. The task here is to write a program to find the longest word in each input sentence(str).

We at gradjobopenings.com provide free job alerts of freshers job drives. In this website we list on campus job openings for freshers and off campus job openings for freshers and also work from home job openings. This is the best website to apply for off campus drive in India. Visit our website for government job alerts and private job alerts. We also list free interview notes and study materials, one of the best interview study website.comfortable to face the interviews:

Note:

The sentence can consist of uppercase, lowercase alphabets, special characters and numbers.

Input format

– First line: String or arbitrary length.

Output format

– First line: An non-negative integer n.

Example 1:

– Input: Knowledge is the greatest gift

– Output: 9

– Explanation: The word “Knowledge” is the longest word in the input statement and it is of size 9. So the output is 9.

Example 2:

– Input: 11223##%%5566778899 Saturn V rocket’s  first stage carries 203,400 gallons (770,000 litres) of kerosene fuel

– Output: 19

– Explanation: Longest word in the sentence is “11223##%%5566778899”. Hence, the output is 19.

Constraints:

str= A-Z, a-z,0-9, special characters

Program in C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    const size_t size = 1000;
    char *inp = (char *) malloc (size * sizeof(char));
    fgets(inp, size, stdin);
    // scanf("%[^\n]s", inp);
    size_t curr_len = 0, max_len = 0, len = strlen(inp);
    for (size_t i = 0 ; i < len ;) {
        while (inp[i++] != ' ' && i <= len) {
            curr_len++;
        }
        if (max_len < curr_len)
            max_len = curr_len;
        curr_len = 0;
    }
    printf("%lu", max_len);
    return 0;
}

Program in C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string inp_string;
    getline(cin, inp_string);
    istringstream sstream (inp_string);
    size_t max_len = 0;
    while(sstream && inp_string != "")
    {
        sstream >> inp_string;
        max_len = max (max_len, inp_string.length() );
    }
    cout << max_len;
}

Program in Java

import java.util.*;
 
class Main
{
    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)
    {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        in.close();
        System.out.println(LongestWordLength(s));
    }
}

Program in Python

if __name__ == '__main__':
    max_len = 0
    for i in input().split():
        l = len(i)
        if max_len < l:
            max_len = l
    print(max_len, end = '')

FOR MORE TCS NQT CODING PROBLEMS 👉👉👉 CLICK HERE

1 thought on “TCS NQT Longest Word Coding Question With Solution”

  1. Pingback: TCS NQT CODING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!