String3
  1. Write a program To extract letters of a word(String)
import java.io.*;
class Extract
{
    public static void main()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a word");
        String wrd= br.readLine();
        int len=wrd.length();
        for (int i=0;i<len;i++)
        {
            char ch=wrd.charAt(i);
            System.out.println(ch);
        }
    }
}

Output:-
Enter a word
Computer

C
o
m
p
u
t
e
r

2. Write a program to extract words of a sentence.

import java.io.*;
class ExtractWord
{
    public static void main()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a sentence");
        String str= br.readLine();
        int len=str.length(); 
        int d=0;
        for (int i=0;i<len;i++)
        {
            char ch=str.charAt(i);
            if(Character.isWhitespace(ch))
            {
                String temp=str.substring(d,i);
                d=i;
                System.out.println(temp.trim());
            }
        }
    }
}

Enter a sentence
This is computer program
This
is
computer
program

LEAVE A REPLY

Please enter your comment!
Please enter your name here