131k views
5 votes
Write code which takes a sentence as an input from the user and then prints the length of the first word in that sentence

User Flosculus
by
4.8k points

1 Answer

3 votes

Answer:

import java.util.*;

class StringProgram

{

public static void main(String s[])

{

Scanner sc = new Scanner(System.in);

System.out.print("Enter a Sentence: ");

String str= sc. nextInt();

// trim the white spaces at the beginning and at the end.

str = str.trim();

int c=0;

int i=0;

while(str.charAt(i++)!=' ')

++c;

System.out.println("Length of the first word in the sentence is: "+c);

}

}

Step-by-step explanation:

User Amitabh
by
5.3k points