Answer to Question #294112 in Java | JSP | JSF for Helen

Question #294112

Write a program to take a String as input then display the words in one column, number of characters in each word in second column and the first letter of each word in third column with appropriate heading.

For example:

Enter a String

Hello world

Words Length First Letter

Hello 5 H

world 5 w


for(i=0;i<s.lastIndexOf(' ');i++) {

      c = s.charAt(i);

      if(c ==' ')

        s1=s1+s.charAt(i+1)+" ";

    } //Use this code for first letter


for(i=0;i<s.length();i++) {

      c = s.charAt(i);

      if (c != ' ')

        n = n+c;

      else {

        System.out.println(n+...);

       } //Use this code for display words and its number of characters


1
Expert's answer
2022-02-04T16:19:08-0500
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a String");
        String s = in.nextLine();
        String s1 = s.charAt(0) + "";
        for (int i = 0; i <= s.lastIndexOf(' '); i++) {
            char c = s.charAt(i);
            if (c == ' ') {
                s1 = s1 + s.charAt(i + 1);
            }
        } //Use this code for first letter

        String n = "";
        System.out.println("Words Length First Letter");
        for (int i = 0, j = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c != ' ') {
                n = n + c;
            }
            if (c == ' ' || i == s.length() - 1) {
                System.out.println(n + " " + n.length() + " " + s1.charAt(j++));
                n = "";
            }

        } //Use this code for display words and its number of characters
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS