Given a string, take the first 2 chars and return the string with the 2 chars added at both the front and back, so "kitten" yields"kikittenki". If the string length is less than 2, use whatever chars are there.
My Own program follows:
public class FrontTwo {
public static void frontT(String str)
{
if(str.length()>=2){
String temp=str.substring(0, 2);
System.out.println(temp+""+str+""+temp);
}
else if(str.length()<2)
{
String temp=str.substring(0);
System.out.println(temp+""+str+""+temp);
}
else
{
System.out.println(str);
}
}
public static void main(String[] args) {
frontT("hai");
}
}
Java programs for beginners, list of programs in java on various topics, interesting programs in java. Java programs on various topics
Sunday, December 16, 2012
Java String Program
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Please post your comments. Your comments make us to write more programs for you.