Java String

Java String provides a lot of concepts that can be performed on a string such as compare, concat, equals, split, length, replace, compareTo, intern, substring etc.

String methods and handling

Example:

Q.1 : WRITE A PROGRAM FOR CHECK WHETHER GIVEN STRING IS PALINDROME OR NOT ?

/*    WRITE A PROGRAM FOR CHECK WETHER GIVEN STRING IS PALINDROME OR NOT */
import java.util.*;
public class softaid
{
	public static void main(String[] args) 
	{
		Scanner s=new Scanner(System.in);
		System.out.print("ENTER YOUR STRING : ");
		String str1=s.nextLine();
		int l=str1.length(),i;
		String str2="";
		for(i=(l-1);i>=0;i-- )
		{
			str2=str2+str1.charAt(i);
		}
		
		if(str1.equals(str2))
		{
			System.out.println("STRING IS PALINDROME   ");
		}
		else
		{
			System.out.println("STRING IS NOT PALINDROME   ");
		}
		
		
	}
}       
  

Q.2 : WRITE A PROGRAM THAT CALCULATE NUMBER OF BLANK'S SPACE IN A GIVEN STRING ?

/*    WRITE A PROGRAM THAT CALCULATE NUMBER OF BLANK'S SPACE IN A GIVEN STRING */
import java.util.*;
public class softaid 
{
	public static void main(String[] args) 
	{
		Scanner s=new Scanner(System.in);
		String str1;
		System.out.print("ENTER ANY STRING  : ");
		str1=s.nextLine();
		int l=str1.length(); // length of given string
		int i,b_space=0;
		for (i=0;i<l;i++)
		{
			if(str1.charAt(i)==' ')
			{
				b_space++;
			}
		}
		
		System.out.print("NUMBER OF BLANK SPACE :  "+	b_space);
		
	}

}

Q.3 : WRITE A PROGRAM TO CALCULATE NUMBER OF 'M' FROM THE GIVEN STRING ?

/*    WRITE A PROGRAM TO CALCULATE NUMBER OF 'M' FROM THE USER GIVEN STRING  */
import java.util.*;
public class softaid 
{
	public static void main(String[] args) 
	{
		Scanner s=new Scanner(System.in);
		String str1;
		System.out.print("ENTER ANY STRING  : ");
		str1=s.nextLine();
		int l=str1.length(); // length of given string
		int i,M=0;
		for (i=0;i<l;i++)
		{
			if(str1.charAt(i)=='M')
			{
				M++;
			}
		}
		
		System.out.print("NUMBER OF Character 'M' :  "+	M);
	}
}

Q.4 : WRITE A PROGRAM TO ACCEPT TWO STRING AND JOIN THEM .. ?

/*    WRITE A PROGRA TO ACCEPT TWO STRING AND JOINE THEM  */
import java.util.*;
public class softaid 
{
	public static void main(String[] args)
	{
		String str1,str2;
		Scanner s=new Scanner(System.in);
		System.out.print("ENTER YOUR FIRST STRING : ");
		str1=s.nextLine();
		System.out.print("ENTER YOUR SECOND STRING : ");
		str2=s.nextLine();
		
		// Concatenation of string (joining them ) 
		String concat = str1.concat(str2);
		
		System.out.println("YOUR FIRST STRING IS : "+str1);
		System.out.println("YOUR FIRST SECOND IS : "+str2);
		System.out.println("YOUR JOINING OF ABOVE STRING  IS : "+concat);
	}
}