Results 1 to 4 of 4

Thread: program to reverse a string in java.

  1. #1
    Join Date
    Jan 2009
    Posts
    721

    program to reverse a string in java.

    Hi,
    I am new to programming language. I recently started learning java language. Yesterday our sir has given us program like write program to reverse a string in java. I use various method but I unable to write that program. That's why I asked this question on this forum. So if you have any idea about this please help me.Thanks in advanced.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: program to reverse a string in java.

    This is the simple program I had ever made. Just try to understand each steps carefully.




    Code:
    public class ReverseST
              {
              public static void main(String[] args) 
                  {
                     String Word = "Welcome to this problem";
                 System.out.println(ReverseString.reverseIt(Word));
                   }
             }
    
             class ReverseS
              {
                public static String reverseIt(String source) 
                {
               int p, pen = source.length();
        StringBuffer dest = new StringBuffer(pen);
    
        for (p = (pen - 1); p >= 0; p--)
          dest.append(source.charAt(p));
        return dest.toString();
      }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: program to reverse a string in java.

    In this program I used array.Just go through it.



    Code:
    public class ReverseString
        {
        public static void main(String[] args)
        {
        String str=args[0];
        String rev = new StringBuffer(str).
    reverse().toString();
        System.out.println("\nString before reverse the word:"+str);
        System.out.println("String after reverse the word:"+rev);
      }     
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: program to reverse a string in java.

    You can used reverse function injava to reverse the program.


    Code:
      public class StringReverse
      {
    
       public static void main(String args[])
      {
    
      String strOgi = "Hello World";
    
      System.out.println(" String before reverse : " + strOgi);
      strOgi = new StringBuffer(strOgi).reverse().toString();
    
    
      System.out.println(" String after reverse : " + strOgi);
    
      }
    
      }

Similar Threads

  1. Java program to iterate a subset of a string.
    By Nadiaa in forum Software Development
    Replies: 5
    Last Post: 19-03-2012, 01:53 PM
  2. Replies: 4
    Last Post: 30-01-2010, 07:23 PM
  3. Java program to reverse the order of LinkedList elements?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 09:44 PM
  4. How to write string data to file using java program?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:26 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,444,675.03357 seconds with 17 queries