Home
| | Sitemap
||Page number :26
replace()
There may be a certain condition when you want to replace a certain character with
another character.
Syntax---- name.replace(‘initial character’, ‘final character’)
class rep
{
public static void main(String args[])
{
String str="mango";
String str2;
str2=str.replace('m','t');
System.out.println("The second String is "+str2);
}
}

reverse()
The reverse() method is used to reverse the contents of a String. class rev { public static void main(String args[]) { StringBuffer frm=new StringBuffer("Sun Microsystems"); frm=frm.reverse(); System.out.println("String in its reverse order is--"+frm); } }
compareTo() method
This method is used to sort strings in Dictionary order. The method returns ‘0’,if two Strings are exactly equal. A value less than ‘0’ is returned if the invoking string comes before the other string in the dictionary. Hence it is absolutely clear that if a value greater than ‘0’ is returned, the invoking string will be next to the other string.
class dict { public static void main(String args[]) { String name="Anjali”; String name2="Ankit"; if(name.compareTo(name2)<0) System.out.println("1st"); else System.out.println("2nd"); } }
As “j” proceeds “k” in the dictionary, in this comparison a value smaller than zero is returned. You can add an IgnoreCase to this method making it unaffected by case changes. Finally the method will be..
name.compareToIgnoreCase(name2)
Examples
As already discussed in chapter five, perfection is achieved by practice and persistence. Here are some examples, you should try to solve without looking at the solutions.
Note: In some programs arrays have been used, skip them until you read that chapter.
Example 1
/* Finding the largest word in a String Example – “I am back” Answer = Back */
// author Nitish Upreti
class word { private String str; private int c,len,l,u,c1,lar; private int arr[]=new int[50];
//class constructor word() { str="I am a proud Indian"; c=0; l=str.length(); }
//Method to find largest word
public void store_lar() { u=0; for(c=0;c<l;c++) { if(c==l-1 || str.charAt(c+1)==' ') { len=(c+1)-u; arr[c1]=len; c1++; u=c+2; } } }
//Calculate the length of Largest no
public void largest_no() { lar=arr[0]; for(c=0;c<arr.length;c++) { if(lar<arr[c]) lar=arr[c]; } }
//Printing largest word public void printlar() { u=0; for(c=0;c<l;c++) { if(c==l-1 || str.charAt(c+1)==' ') { len=(c+1)-u; if(len==lar) { for(int x=u;x<=c;x++) System.out.print(str.charAt(x)); } u=c+2; } } } }
class largest_word { public static void main(String args[]) { word obj=new word(); obj.store_lar(); obj.largest_no(); obj.printlar(); } }
Example 2
/* A program to sort names in Dictionary order */ // author Nitish Upreti
class dictionary
{ static String arr[]={"Ankit","Dhanajay","Vinod","Priyanka","Nitish"};
public static void main(String args[]) { for(int c=0;c<arr.length;c++) { for(int c1=c;c1<arr.length;c1++) { if(arr[c].compareToIgnoreCase(arr[c1])>0) { String temp=arr[c]; arr[c]=arr[c1]; arr[c1]=temp; } } System.out.println("Sorted String is"); System.out.println(arr[c]); } } }
/* A program to print the character which has largest ASCII value as well as the char
itself E.G- education Output- u 117 */
// author Nitish Upreti
class lar_ch_val { String str; int c,lar,len,val=0,temp; char ch;
public lar_ch_val() { str="BILL GATES"; c=0; len=str.length(); }
public void lar() { for(c=0;c<len;c++) {
ch=str.charAt(c); temp=(int)ch; //explicit conversion of data types if(temp>val) // finding max ASCII value val=temp; } }
public void out() { ch=(char)val; //Getting no. back to char form System.out.print(ch); System.out.print("\t"+val); }
public static void main(String sv[]) { lar_ch_val obj=new lar_ch_val(); obj.lar(); obj.out(); } }
page 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29