reverse string in java


import java.util.Scanner;
public class StringReverse {
public static void main(String[] args) {
String original = “”;
String reverse = “”;
Scanner in = new Scanner(System.in);
System.out.println(“Enter a string to reverse”);
original = in.nextLine();
int length = original.length();
for (int i = length – 1; i >= 0; i–)
reverse = reverse + original.charAt(i);
System.out.println(“Reverse string is: ” + reverse);
}
}

Explain about String Class and its constructors with examples?


java.lang Class String

 java.lang.Object

          java.lang.String

 All Implemented Interfaces:

SerializableCharSequence,Comparable<String>.

 ¤ public final class String

extends Object  

implements Serializable, Comparable<String>, CharSequence

Since:

JDK1.0

Definition:

        In general String can be defined as it is a sequence of characters. Strings are constant; their values cannot changed in the same memory after they are created, so String is defined as it is an immutable sequence of characters.

¤What are the possible ways to create String Object?

              String object can be created in two ways

                    1. By assigning String literal directly

                    2.By using any one of the String class constructors

======================================================

 Constructors of  String Class:

             String class supports several constructors.

      1.String s = new String();

            To create an empty String object, no null String Object.

      2.String s = new String(String literal);

            To create an equivalent String Object for the given String literal on       the heap.

      3.String s = new String(StringBuffer sb)

            To create String Object with the StringBuffer Object value.

     4.String s = new String(StringBuilder sb)

            To create String Object with the StringBuilder Object value.

            Version:1.5

     5.String s = new String( Char [] ch)

          To create an equivalent String Object for the given Char[].

Example:

      public class SringDemo{

      public static void main(String[] args){

       Char[] ch =  {‘a’,’b’,’c’,’d’};

        String s = new String(ch);

        System.out.println(s);

       }//main

}//class

OutPut:abcd

6.String s = new String( byte[] b)

              To create String Object with the given byte array values.

Example:

      public class SringDemo{

      public static void main(String[] args){

       byte[] b =  {100,101,102,103 };

        String s = new String(b);

        System.out.println(s);

       }//main

}//class

OutPut : defg

7. String s = new String( Char [] ch, int offset, int count)

            To create String Object with the given char array values starts with offset by including with the given count of characters

 Throws:

IndexOutOfBoundsException

 Example:

       public class SringDemo{

         public static void main(String[] args){

       Char[] ch =  {‘a’,’b’,’c’,’d’,’e’,’f’};

        String s = new String(ch,2,3);

        System.out.println(s);

       }//main

}//class

Output:cde

 8. String s = new String( byte[] b, int offset, int count)

            To create String Object with the given byte array values starts with offset by including with the given count of bytes

 Throws:

IndexOutOfBoundsException

Version:

JDK1.1

 Example:

      public class SringDemo{

      public static void main(String[] args){

       byte[] b =  {65,66,67,68,69,70};

        String s = new String(s);

        System.out.println(s);

        String s1 = new String(b,2,3);

        System.out.println(s1);

     }//main

}//class

   OutPut: ABCDEF

                 CDE

======================================================

String Class Methods:

o charAt:

       1.public char charAt(int index)  

  • Returns the char value at the specified index. An index ranges from 0 to length() – 1.

  • The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

  • If the char value specified by the index is a surrogate, the surrogate value is returned.

Specified by:

Parameters:

  • index – the index of the char value.

Returns:

  • the char value at the specified index of this string. The first char value is at index 0.

Throws: IndexOutOfBoundsException

  • If the index argument is negative or not less than the length of this string.


 

java.lang.String class Example by using equals() method


class StringDemoEq
{
    public static void main(String[] args)
    {
        String s1 = new String(“NAG”);
        String s2 = new String(“nag”);
        String s3 = “nag”;
        String s4 = “NAG”;
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s4));
        System.out.println(s2.equals(s3));
        System.out.println(s2.equals(s4));
        System.out.println(s3.equals(s4));

    }
}
StringDemoEq

 

java.lang.String class Example by using “==” operator


class StringDemoExm
{
public static void main(String[] args)
{
String s1 = new String(“nag”);
String s2 = new String(“nag”);
String s3 = “nag”;
String s4 = “nag”;
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s1==s4);
System.out.println(s2==s3);
System.out.println(s2==s4);
System.out.println(s3==s4);
}
}

StringDemoExam

Example of length() method (or)Example of length() method in java (or) Example length() method of string class


1: public class StringDemo

2: {

3: public static void main(String[] args)

4: {

5: String string1 = new String();

6: String string2 = new String(“http://www.chennarapu.wordpress.com“);

7: String string3 = “NagaRaju“;

8: String string4 =””;

9: String string5 = null;

10: System.out.println(“string1 : “+string1.length());

11: System.out.println(“string2 : “+string2.length());

12: System.out.println(“string3 : “+string3.length());

13: System.out.println(“string4 : “+string4.length());

14: System.out.println(“string5 : “+string5.length());

15:

16: }

17: }

18:

OutPut :

image

Q) Explain trim() method of String class with an examples?


Q) Explain trim() method of String class with an examples?

public String trim()

 public class StringDemo22{

            public static void main(String[] args){

String s1 = new String(”    http://www.chennarapu.wordpress.com    “);

                         System.out.println(s1);

                        System.out.println(s1.trim());

   }

}

Output :

D:\NAG\STRINGS>javac StringDemo22.java

D:\NAG\STRINGS>java StringDemo22

    http://www.chennarapu.wordpress.com

http://www.chennarapu.wordpress.com

 

Q) Explain about lastIndexOf(int ch) method of String class with an examples?


Q) Explain about lastIndexOf(int ch) method of String class with an examples?

               public int lastIndexOf(int ch)

public class StringDemo20{

            public static void main(String[] args){

String s1 = new String(“www.chennarapu.wordpress.com”)

                                   System.out.println(s1.lastIndexOf(‘n’));

                                    System.out.println(s1.lastIndexOf(‘c’));

    }

}

Output :

D:\NAG\STRINGS>javac StringDemo20.java

D:\NAG\STRINGS>java StringDemo20

8

25

Q) Explain about indexOf(int ch, int fromIndex) method of String class with an examples?


Q) Explain about indexOf(int ch,

                                                   int fromIndex) method of String class with an examples?

 public int indexOf(int ch,

                                      int fromIndex)

public class StringDemo19{

            public static void main(String[] args){

String s1 = new String(“www.chennarapu.wordpress.com”);

                        System.out.println(s1.indexOf(‘n’,1));

                        System.out.println(s1.indexOf(‘c’,3));

  }

}

Output :

D:\NAG\STRINGS>javac StringDemo19.java

D:\NAG\STRINGS>java StringDemo19

7

4

Q) Explain about endsWith(String suffix) method of String class with an examples?


Q) Explain about endsWith(String suffix) method of String class with an examples?

public boolean endsWith(String suffix)

Tests if this string ends with the specified suffix.

public class StringDemo17 {

     public static void main(String[] args) {

          String s1 = new String(“www.chennarapu.wordpress.com”);

         System.out.println(s1.endsWith(“m”));

          System.out.println(s1.endsWith(“w”));

  }

}

Output :

D:\NAG\STRINGS>javac StringDemo17.java

D:\NAG\STRINGS>java StringDemo17

true

false

Q) Explain about indexOf(int ch) method of String class with an examples?


Q) Explain about indexOf(int ch) method of String class with an examples?
  • public int indexOf(int ch)

  • returns first occurrence index or , -1 if the character does not occur.

            public class StringDemo18{

            public static void main(String[] args){

            String s1 = new String(“www.chennarapu.wordpress.com”);

            System.out.println(s1.indexOf(‘c’));

            System.out.println(s1.indexOf(‘i’));

            }

            }

            Output :

            D:\NAG\STRINGS>javac StringDemo18.java

            D:\NAG\STRINGS>java StringDemo18

            4

            -1