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
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
}
}
Comments
Leave a comment