package com.javainchennai;
import java.util.Scanner;
public class StringReverse {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String str=sc.nextLine();
int strLen=str.length();
char c[]=new char[20];
for(int i=0,j=strLen-1;i<strLen;i++,j--)
{
c[i]=str.charAt(j);
System.out.print(c[i]);
}
}
}
package com.javainchennai;
import java.util.Scanner;
public class Stringpolindrome {
public static void main(String[] args) {
int i,j=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your String:-");
String str=sc.next();
char c[]=new char[20];
c=str.toCharArray();
i=str.length();
--i;
while(j<=i)
{
if(c[i]!=c[j])
{
System.out.println("The given string "+ str +" is not polindrome");
System.exit(0);
}
++j;
--i;
}
System.out.println("The given string" + str +"is polindrome");
}
}
The given string javainchennai is not polindrome
The given string malayalam is polindrome
FINDING THE LENGTH OF A STRING
package com.javainchennai;
import java.util.Scanner;
public class StringLength {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter your String:-");
String str=sc.nextLine();
char c[]=new char[10];
c= str.toCharArray();
int count=0;
try{
for( count=0;c[count] !='\0'; count++);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(count);
}
}
Please Enter your String:-
The Length of the Given String is: 13
FINDING THE NUMBER OF WORDS IN A STRING
package com.javainchennai;
import java.util.Scanner;
public class NoOfWordsInAString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your string:-");
String str=sc.nextLine();
int strLen=str.length();
int word=1;
for(int i=0;i<strLen-1;i++)
{
if((str.charAt(i))==' ')
{
word++;
}
}
System.out.print("The Number of words in the Given String are :" +word);
}
}
The Number of words in the Given String are :2
Before going into programs a small introduction on Strings
If there is any mistakes in this article please write me blog4helping@gmail.com
======================
-:STRINGS:-
======================
1.String is a class in Java
2.String means collection of characters
3.String is a sequence of characters enclosed in double Quotes.
4.Java Strings are sequence of Unicode characters
5.String class produces Immutable objects.
6.String class does not have a methods that let you change a characters in an existing String.
7.It is like number 5 , the String "HELLO" we cannot change it.
Sting Objects can be created in 2 ways
1. without using new operator
String str1="javainchennai";
2. with using new operator
String str2=new String("javainchennai");
The memory space is divided in to 2 parts
1.constant area
2.non-constant area
If you create object without using new operator this will be saved in constant area
String str1="javainchennai";
If you create object with using new operator this will be saved in non-constant area
String str2=new String("javainchennai");
Here Constant area will not allow duplicates
where as non-constant area will allow duplicates.
Do Remember inside a String class the toString() method is overided
Here if you try to print reference variable
String s1="javainchennai";
System.out.println(s1);
The output is javainchennai instead of String@ff2201(classname @ hexadecimal value)
equals() method also overrided to check 2 objects based on the value(content) it contains
observe the program carefully and read the explanation:-
1.package com.qb;
2.public class StringTest {
3. public static void main(String[] args) {
4. String str1="javainchennai";
5. String str2="javainchennai";
6. String str3=new String("javainchennai");
7. String str4=new String("javainchennai");
8. System.out.println(str1==str2);
9. System.out.println(str1==str3);
10. System.out.println(str2==str3);
11. System.out.println(str3==str4);
12. System.out.println(str1.equals(str2));
13. System.out.println(str1.equals(str3));
14. System.out.println(str2.equals(str3));
15. System.out.println(str3.equals(str4));
16. }
17. }
output:-
true
false
false
false
true
true
true
true
explanation:-
If you say 5==5 is true, here two are primitive literals so == operator(checks content) prints true.
If you use == operator to compare two object references it will see whether both references are pointing to same object or different.
If both references are pointing to same object it(== operator) returns true otherwise false.
observe line4 and line5 ,here actually I created String object without using new operator so, str1 and str2 both references are pointing to same object (javainchennai). (constant area will not allow duplicates)
so, str1==str2 returns true.
observe line4 and line5 ,here actually I created String object by using new operator so, str3 and str4 both references are pointing to different objects (javainchennai).(non constant area allows duplicates).
So, str3==str4 returns false.
Observe the same effect with equals() method it returns true all time .(because it is overrided to compare based on content inside object)
PROGRAMS:-
program:-
1.public class StringTest {
2. public static void main(String[] args) {
3. System.out.println(3+5);
4. System.out.println("3"+5);
5. System.out.println(3+"555");
6. System.out.println(3+2+5+"4444");
7. System.out.println("35"+111+"99");
8. System.out.println("42"+11+32+"77");
9. System.out.println("100"+"222");
}
}
OUTPUT:-
8
35
3555
104444
3511199
42113277
100222
Explanation:-
-------------
In Java there is no operator overloading concept.
+ is the only one operator overloaded to add 2 integers as well as 2 strings
In the above example we added 2 primitives that is common ,
when we added String with any primitives only String object is produced.
observe the output of lines 6,7,8,9.
DIFFERENCES BETWEEN STRING , STRINGBUFFER AND STRINGBUILDER
String:-
------------------
1.String class introduced in jdk1.0
2.String class is a final class and implements Serializable,Comparable and CharSequence interfaces
3.String Objects can be created in two ways
a.Using new operator
constructors to create String ob
b.Without using new operator
4.-----
5. String class has several
jects(both using new or without using new operator)
a.String s1="";
that it represents an empty char
b.String s2=new String("");
Initializes a String object so
acter sequence.
c.String s1="javainchenna";
resents the same sequence of characters as the argument
5.String class has no append method.
6.There
Initializes a String object so that it re
p is no delete method to delete a characters from .
7.StringBuilder objects are Non-Synchronized.
------------------
StringBuffer:-
------------------
StringBuffer and StringBuilder has the same methods:-
1.String class introduced in jdk1.0
lements Serializable and CharSequence interfaces
.StringBuffer Objects cannot be cre
3
2.String class is a final class and im
pated without using new operator
StringBuffer s2="javainchennai";(wrong way)
Constructs a StringBuffer with no
4.use new operator to create objects of StringBuffer
StringBuffer sb1=new StringB
u characters in it and an initial capacity of 16 characters.
g representation of the any type(primitive or derived) argument to the sequence.
.There is a delete method to delete a characters from this
6
5.StringBuffer class has append() method , It appends the stri
nobject.
7.StringBuffer objects are Synchronized.StringBuffer was being used by a single thread.
------------------
StringBuilder:-
------------------
.String class introduced in jdk1.5
1,
tring class is a final class and implements Serializable and CharSequence interfaces
3
2.
S.StringBuffer Objects cannot be created without using new operator
or to create objects of StringBuilder
StringBuilder s3="javainchennai";(wrong way)
4.use new opera
tgBuilder sb1=new StringBuilder();
th no characters in it and an initial capacity of 16 characters.
ngBuilder class has appen
5.Str
i
Constructs a StringBuilder w
id() method,It appends the string representation of the any type(primitive or derived) argument to the sequence.
d.
6. There is a delete method to delete a characters from this object.
7.StringBuilder objects are Non-Synchroniz
e
FIND BIGGEST OF 3 NUMBERS:
import java.util.Scanner;
public static void main(String[] args)
System.out.println("Enter 3 numbers");
Scanner sc=new Scanner(System.in);
System.out.println("The biggest of 3 numbers:"+max);
The biggest of 3 numbers:98
FIND GIVEN NUMBER IS ODD OR EVEN:
import java.util.Scanner;
public static void main(String[] args)
System.out.println("Enter the number");
Scanner sc=new Scanner(System.in);
System.out.println("Number is ODD");
System.out.println("Number is EVEN");
Number is EVEN
FIND GIVEN NUMBER IN PERFECT SQUARE OR NOT:
import java.util.Scanner;
public class perfectsquare
public static void main(String[] args)
System.out.println("Enter a number");
Scanner sc=new Scanner(System.in);
double num1=Math.sqrt(num);
System.out.println(num + "is a perfect square");
System.out.println(num + "is not a perfect square");
WRITE A PROGRAM TO FIND GIVEN NUMBER IS PALINDROME OR NOT
import java.util.Scanner;
public class PalindromeOfNumber
public static void main (String[] args)
System.out.println("Enter a number:");
Scanner sc=new Scanner(System.in);
{ System.out.println("The given number "+num1+" is palindrome");
System.out.println("The given number "+num1+" is not palindrome");
The given number 1523 is not palindrome
WRITE A PROGRAM TO FIND GIVEN NUMBER IS PRIME OR NOT
import java.util.Scanner;
public static void main(String[] args)
System.out.println("Enter the number:");
Scanner sc=new Scanner(System.in);
System.out.println("The given number is prime:");
System.out.println("The given number "+num+" is not prime");
System.out.println("The given number "+num+" is prime");
The given number 5 is prime
The given number 55 is not prime
WRITE A PROGRAM TO DISPLAY MULTIPLICATION TABLE
import java.util.Scanner;
public class multiplicationtable
public static void main(String[] args)
System.out.println("Enter the table number and the count value:");
Scanner sc=new Scanner(System.in);
System.out.println("The table is ...");
for(int i=1;i<=count;i++){
System.out.println(tnum+"*"+i+"=" + i*tnum);
Enter the table number and the count value:
WRITE A PROGRAM TO FIND COMMAND LINE NUMBER COUNT
public class CommandLineNumbersCount
public static void main(String[] args)
Java CommandLineNumbersCount 10 5 23 4 2
java CommandLineNumbersCount 21 3 36 4
WRITE A PROGRAM TO SORT THE DIGITS OF A NUMBER IN ASCENDING ORDER
import java.util.Scanner;
public class SortDigitsInInteger
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number:-");
System.out.println("The digits in number ascending order:");
System.out.print(sort[j]+" ");
The digits in number ascending order:
WRITE A PROGRAM TO SORT A NUMBER IN ASCENDING ORDER
import java.util.Scanner;
public class SortNumbersInAscendingOrder
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number of elements:-");
System.out.println(" Enter Elements:- ");
for(int i=1;i<=limit;i++)
for(int i=0;i<limit-1;i++)
System.out.println("The sorted list is:-");
for( int i=0;i<limit;i++)
System.out.print(" "+a[i]);
Enter Number of elements:-
PROGRAMMING WITH RECURSION
Fibonacci series
import java.util.Scanner;
public class FibonacciSeries
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Number:-");
System.out.println("The fibo series is:- ");
private static void fiboSeries(int x,int f,int f1)
System.out.print(" " + (f+f1));
1 2 3 5 8 13 21 34
Tribonacci series
mport java.util.Scanner;
i
ublic class TribonacciSeries
p
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Number:-");
System.out.println("The tribo series is:- ");
private static void triboSeries(int x,int f,int f1,int f2)
System.out.print(" " + (f+f1+f2));
triboSeries(--x,f1,f2,f+f1+f2);
WRITE A PROGRAM TO FIND THE POWER OF THE NUMBER
import java.util.Scanner;
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter the values of X and N:-");
double x=sc.nextDouble();
System.out.println(power(x,n));
private static double power(double x,int n)
pow= ((n==0)?1:x*power(x,n-1));
Enter the values of X and N:-
Enter the values of X and N:-
--------------------------------------------------------------------------------------------------------------
PROGRAM TO READ AN INTEGER AND REDUCE IT TO A SINGLE DIGIT
import java.util.Scanner;
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Number:-");
System.out.println(" " + calculateDigit(num));
private static long calculateDigit(long num)
return (num/10 == 0 ? num : calculateDigit(num/10 + num % 10));
WRITE A PROGRAM TO DISPLAY AMOUNT INTO 500’S,100’S,50’S,20’S,10’S,5’S,2’S,1’S IN A GIVEN NUMBER
import java.util.Scanner;
public static void main(String[] args)
System.out.println("Enter the amount in rupees:-");
Scanner sc=new Scanner(System.in);
System.out.print("\n The no .of five hundreds are:- "+ a);
System.out.print("\n The no .of hundreds are :-"+ a);
System.out.print("\n The no .of fiftie's are "+ a);
System.out.print("\n The no .of twenty's are "+ a);
System.out.print("\n The no .of ten's are "+ a);
System.out.print("\n The no .of five's are "+ a);
System.out.print("\n The no .of two's are "+ a);
System.out.print("\n The no.of one's are"+ a);
Enter the amount in rupees:-
The no .of five hundreds are 3
The no .of hundreds are 18
The no .of fiftie's are 37
The no .of twenty's are 92
The no .of five's are 370
WRITE A PROGRAM TO SORT A NUMBER IN ASCENDING ORDER
import java.util.Scanner;
public class SortNumbersInAscendingOrder
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number of elements:-");
System.out.println(" Enter Elements:- ");
for(int i=1;i<=limit;i++)
for(int i=0;i<limit-1;i++)
System.out.println("The sorted list is:-");
for( int i=0;i<limit;i++)
System.out.print(" "+a[i]);
Enter Number of elements:-
--------------------------------------------------------------------------------------------------------------
WRITE A PROGRAM TO CONVERT INTEGER INTO IT’S EQUIVALENT NAME
import java.util.Scanner;
public class IntegerToItsEqualentName
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number");
case 0:System.out.print("ZERO ");
case 1:System.out.print("ONE ");
case 2:System.out.print("TWO ");
case 3:System.out.print("THREE ");
case 4:System.out.print("FOUR ");
case 5:System.out.print("FIVE ");
case 6:System.out.print("SIX ");
case 7:System.out.print("SEVEN ");
case 8:System.out.print("EIGHT ");
case 9:System.out.print("NINE ");
PROGRAM FOR SWAPPING TWO NUMBERS USING THREE VARIABLES
import java.util.Scanner;
public static void main(String[] args)
System.out.println ("enter 2 numbers");
Scanner sc=new Scanner(System. in);
System.out.println("Before swapping");
System.out.println("num1:"+num1+"\n"+"num2:"+num2);
System.out.println("After swapping");
System.out.println("num1:"+num1+"\n"+"num2:"+num2);
PROGRAM FOR SWAPPING TWO NUMBERS WITHOUT USING THIRD VARIABLE
import java.util.Scanner;
public static void main(String args[])
System.out.println("Enter 2 numbers");
Scanner sc=new Scanner(System.in);
System.out.println("Before swapping");
System.out.println("num1:"+num1+"\n"+"num2:"+num2);
System.out.println("After swapping");
System.out.println("num1:"+num1+"\n"+"num2:"+num2);
SWAPPING 2 NUMBERS WITHOUT USING ARITHMETIC OPERATORS
import java.util.Scanner;
public static void main(String[] args)
System.out.println("Enter 2 numbers");
Scanner sc=new Scanner(System.in);
System.out.println("Before swapping");
System.out.println("num1:"+num1+"\n"+"num2:"+num2);
System.out.println("After swapping");
System.out.println("num1:"+num1+"\n"+"num2:"+num2);
WHETHER IT IS C , C++, C# OR JAVA , WORKING WITH THESE KIND OF PROGRAMS GIVES YOU LOT OF FUN :-
Write a program to display the following output
import java.util.Scanner;
public static void main(String[] args)
System.out.println("Enter the limit:-");
Scanner sc=new Scanner(System.in);
for(int i=1;i<=limit;i++)
System.out.print( i +" ");
Write a program to display the following output
package com.vel.nestedforloop;
import java.util.Scanner;
public static void main(String[] args)
System.out.println("Enter the limit:-");
Scanner sc=new Scanner(System.in);
for(int i=1;i<=limit;i++)
System.out.print( j +" ");
}
Write a program to display the following output
package com.vel.nestedforloop;
import java.util.Scanner;
public static void main(String[] args)
System.out.println("Enter the limit:-");
Scanner sc=new Scanner(System.in);
for(int i=1;i<=limit+1;i++)
System.out.print( k++ +" ");
WRITE A PROGRAM TO DISPLAY THE FOLLOWING OUTPUT
3 2 1 0 1 2 3
import java.util.Scanner;
public static void main(String[] args)
System.out.println("Enter Limit:-");
Scanner sc= new Scanner(System.in);
Write a program to display the following output
import java.util.Scanner;
public class NumberGenerator
public static void main(String[] args)
System.out.println("ENTER THE NUMBER TO BE DISPLAYED :-");
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE SIZE :-");
ENTER THE NUMBER TO BE DISPLAYED :-
WRITE A PROGRAM TO DISPLAY THE FOLLOWING OUTPUT
import java.util.Scanner;
public class GenerateNumber1
public static void main(String[] args)
System.out.println( "ENTER THE LIMIT:-");
Scanner sc=new Scanner(System.in);
Write a program to print the following output
import java.util.Scanner;
public class GenerateNumber1
public static void main(String[] args)
System.out.println( "ENTER THE LIMIT:-");
Scanner sc=new Scanner(System.in);
System.out.print(x +" ");
Write a program to print the following output with the given character
ENTER THE CHARACTER TO BE DISPLAYED:-
import java.util.Scanner;
public class GenerateNumber1
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE CHARACTER TO BE DISPLAYED:-");
System.out.println( "ENTER THE LIMIT:-");
}
.Write a program to print the following output with the given character
ENTER THE CHARACTER TO BE DISPLAYED:-
import java.util.Scanner;
public class GenerateNumber1
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE CHARACTER TO BE DISPLAYED:-");
System.out.println( "ENTER THE LIMIT:-");
if(i==1||j==1||j==n||i==n)
Program to read an integer and reduce it to a single digit
import java.util.Scanner;
public static void main(String[] args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Number:-");
System.out.println(" " + calculateDigit(num));
private static long calculateDigit(long num)
return (num/10 == 0 ? num : calculateDigit(num/10 + num % 10));