Skip to main content

Java String functions Exercise

1.Write a program to arrange a given set of names in alphabetical order
import java.util.Arrays;
class namesInAlphabeticalOrder{
public static void main(String args[]){

String names[]={"Sudeep","Rethin","george","Anu"};
System.out.println("before Sorting");
for(int i=0;i<names.length;i++)
   System.out.println(i+":"+names[i]);


System.out.println("After Sorting");
Arrays.sort(names);
for(int i=0;i&ltnames.length;i++)
   System.out.println(i+":"+names[i]);

}
} 
2.Write a program to display the number of words in a given sentence
3.Accept a line of text.find the reverse of each word and display the string
class countWord{
public static void main(String args[]){
String s="sudeep cv always find a way to success";
String[] s1=new String[s.length()];
s1=s.split(" ");


System.out.println("Number of words in s is:"+s1.length);

for(int i=0;i<s1.length;i++){

StringBuilder str=new StringBuilder(s1[i]);
System.out.print(str.reverse()+" ");
}
}

}

Comments