Strings in Java

1.To input a string from the user and display it in console

class bikal
{
   public static void main(String args[])
  {
     char c;
     StringBuffer string=new StringBuffer();
     System.out.println("Enter a string");
     try{
     while((c=(char)System.in.read())!='\n')
     {
       string.append(c);
     }
     }
     catch(Exception e)
     {
   
       System.out.println("Error");
     }
      System.out.println("You have entered");
  System.out.println(string);
   }
}


2.Concatenate two strings entered by user and display it in console

import java.util.Scanner;
class bkl
{
  static  public void main(String [] args)throws Exception
  {
    Scanner in=new Scanner(System.in);
    String string1;
    String string2;
    System.out.println("Enter the two Strings to be concatenated");
    string1=in.nextLine();
    string2=in.nextLine();
    System.out.println("The Concatenated String is: "+string1+string2);  
  }
}



3.Convert a string entered by user to upper case and display the result in console

import java.util.Scanner;
class bkl
{
  static  public void main(String [] args)throws Exception
  {
    Scanner in=new Scanner(System.in);
    String string1;
    System.out.println("Enter a Strings to be into upper case");
    string1=in.nextLine();
    string1=string1.toUpperCase();
    System.out.println("The resulting uppercase String is: "+(string1));  
  }
}



4.Convert a string entered by user to lower case

//and display the result in console
import java.util.Scanner;
class bkl
{
  static  public void main(String [] args)throws Exception
  {
    Scanner in=new Scanner(System.in);
    String string1;
    System.out.println("Enter a Strings to be Converted into Lower case");
    string1=in.nextLine();
    string1=string1.toLowerCase();
    System.out.println("The resulting Lowerrcase String is: "+(string1));  
  }
}



5.To reverse a string entered by the user and display it in console

import java.lang.StringBuffer;
import java.util.Scanner;
class bkl
{
  static  public void main(String [] args)throws Exception
  {
    Scanner in=new Scanner(System.in);
    String string1;
    System.out.println("Enter a Strings to be reversed.");
    string1=in.nextLine();
    String string2=new StringBuffer(string1).reverse().toString();
    System.out.println("The reversed String is: "+(string2));  
  }
}



6.Check whether a given string is a palindromeor not

import java.lang.StringBuffer;
import java.util.Scanner;
class bkl
{
  static  public void main(String [] args)throws Exception
  {
    Scanner in=new Scanner(System.in);
    String string1;
    System.out.println("Enter a Strings to be checked for palindrome.");
    string1=in.nextLine();
    String string2=new StringBuffer(string1).reverse().toString();
    if((string1.compareTo(string2))==0)
    System.out.println("The entered String is palindrome");
    else
    System.out.println("The entered String is not palindrome");
   
  }
}



7.Albhabetic ordering of strings.

import java.util.Scanner;
class arr
{
  public static void main(String [] args)throws Exception
  {
    int i,n,j;
    String temp;
    String string[]=new String[30];
    Scanner in=new Scanner(System.in);
    System.out.println("Enter the number of Strings to be sorted:");
    n=in.nextInt();
    System.out.println("Enter "+n+" Strings:");
    for(i=0;i<n;i++)
    {
      string[i]=in.next();
    }
    for(i=0;i<(n-1);i++)
     {
      for(j=0;j<(n-1);j++)
      {
        if((string[j].compareTo(string[j+1]))>0)
        {
          temp=string[j];
          string[j]=string[j+1];
          string[j+1]=temp;
        }
      }
    }
 
 
    System.out.println("The sorted strings is as follows:");
    for(i=0;i<n;i++)
    {
      System.out.println("  "+string[i]);
    }
 
 
   }

}

0 comments:

Post a Comment