Give a program in JAVA for word extraction .
Very urgent please answer fast .
Answers
import java.util.*;
class word_extraction
{
public void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s=sc.nextLine();
s=s+" ";
int l=s. length();
String w="";
for(int i=0;i<l;i++)
{
char ch=s. charAt(i);
if(ch!=' ')
{
w=w+ch;
}
else
{
System.out.println(w);
w="";
}
}
}
}
NOTE :
The above code is applicable for word extraction .
In word extraction we take an user input as a sentence .
Our task is to extract the words and print it .
The task is done by using string functions .
Character functions are also involved in the above code .
First of all we calculate the length of the string .
Before that we add a space at the end of the string .
Now we run a for-loop from 0 till l ( the length of the string ) .
We extract every character by using the character functions .
Then we run an if-else statement .
If the character is a white space , we will print the word .
If the character is not a white space , we will add and concat the characters .
Hope it helps you!!!.
^_^