Write a program to implement all string operations.

import java.util.*;
class StringOperation
{
    public static void main(String[] args)
      {
        String first="",second="";
        Scanner sc=new Scanner(System.in);
            System.out.println("String Operation");
            System.out.println();
            System.out.print("Enter the first Sting: ");
             first=sc.nextLine();
        System.out.print("Enter the second Sting: ");
              second=sc.nextLine();
            System.out.println("The strings are: "+first+" , "+second);
         System.out.println("The length of the first string is :"+first.length());
                System.out.println("The length of the second string is :"+second.length());
            System.out.println("The concatenation of first and second string is :"+first.concat(" "+second));
             System.out.println("The first character of " +first+" is: "+first.charAt(0));
            System.out.println("The uppercase of " +first+" is: "+first.toUpperCase());
            System.out.println("The lowercase of " +first+" is: "+first.toLowerCase());
            System.out.print("Enter the occurance of a character in "+first+" : ");
            String str=sc.next();
             char c=str.charAt(0);
            System.out.println("The "+c+" occurs at position " + first.indexOf(c)+ " in " + first);
             System.out.println("The substring of "+first+" starting from index 3 and ending at 6 is: " + first.substring(3,7));
            System.out.println("Replacing 'a' with 'o' in "+first+" is: "+first.replace('a','o'));       
            boolean check=first.equals(second);
            if(!check)
                System.out.println(first + " and " + second + " are not same.");
            else
                System.out.println(first + " and " + second + " are same."); 
    }
}

Output

C:\Users\Jaisha\Desktop\Java>javac StringOperation.java

C:\Users\Jaisha\Desktop\Java>java StringOperation
String Operation

Enter the first Sting: The World is Beautiful.
Enter the second Sting: Learn to enjoy every moment.
The strings are: The World is Beautiful. , Learn to enjoy every moment.
The length of the first string is :23
The length of the second string is :28
The concatenation of first and second string is :The World is Beautiful. Learn to enjoy every moment.
The first character of The World is Beautiful. is: T
The uppercase of The World is Beautiful. is: THE WORLD IS BEAUTIFUL.
The lowercase of The World is Beautiful. is: the world is beautiful.
Enter the occurance of a character in The World is Beautiful. : e
The e occurs at position 2 in The World is Beautiful.
The substring of The World is Beautiful. starting from index 3 and ending at 6 is:  Wor
Replacing 'a' with 'o' in The World is Beautiful. is: The World is Beoutiful.
The World is Beautiful. and Learn to enjoy every moment. are not same.

2 comments: