Search This Blog

Sunday, December 29, 2013

Ques:-Input a sentences,frame a new word by adding initial of each word and also check whether the word is palindrome word or not.


/*Input a sentences,frame a new word by adding initial of each word and
*also check whether the word is palindrome word or not. */
import java.io.*;
class palindrome_string
{
public static void main(String arg[])throws IOException//Main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String st,temp="",temp1="";
System.out.println("Enter a sentence");//Inputing A string using Buffered Reader
st=br.readLine();
st=" "+st;
int len1=st.length();
for(int i=0;i<len1;i++)
{
char ch=st.charAt(i);//Extracting initials
if(ch==' ')
{
char ch1=st.charAt(i+1);
temp=temp+ch1;//storing on another variable i.e. new string
}
}
int len2=temp.length();
for(int j=len2-1;j>=0;j--)//reversing string
{
char ch1=temp.charAt(j);
temp1=temp1+ch1;
}
if(temp1.equalsIgnoreCase(temp))//checking whether string is same or not
{
System.out.println("Palindrome Word '"+temp1+"'");
}
else
{
System.out.println("Not a palindrome word '"+temp1+"'");
}
}
}

No comments:

Post a Comment