/* related posts with thumb nails */

A program to perform the following operations on strings through interactive input. a) Sort given strings in alphabetical order.:

import java.io.*;

class StringTest

{

public static void main(String as[]) throws Exception

{

int i,j,n,x;

String temp,s1,s2;

String s[]=new String[10];

InputStreamReader isr=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(isr);

do

{

System.out.println("\n 1.Sorting\n 2.Substring\n 3.Uppercase");

System.out.println("\n 1.Enter your choice:");

x=Integer.parseInt(br.readLine());

}while(x<1||x>3);

if(x==1||x==3)

{

System.out.println("How many Strings:");

n=Integer.parseInt(br.readLine());

System.out.println("Enter strings:\n");

for(i=0;i

s[i]=br.readLine();

}

if(x==1)

{

for(i=0;i

for(j=i+1;j

if(s[i].compareTo(s[j])>0)

{

temp=s[i];

s[i]=s[j];

s[j]=temp;

}

System.out.println("After Sorting:");

for(i=0;i

System.out.print(s[i]+"\t");

}

else if(x==2)

{

System.out.println("Enter main string:");

s1=br.readLine();

System.out.println("Enter sub string:");

s2=br.readLine();

if(s1.indexOf(s2)!=-1)

System.out.println(s2 +" is sub string of"+ s1);

else

System.out.println(s2 +" is not a sub string of "+ s1);

}

else

{

System.out.println("Strings in Uppercase");

for(i=0;i

System.out.print("\t"+s[i].toUpperCase());

}

}

}

/* Output: */

1.Sorting

2.Substring

3.Uppercase

Enter your choice:1

How many Strings:3

Enter strings:

mango

apple

banana

After Sorting:

apple banana mango

Related Topics:

0 comments:

Post a Comment