Posted by: zeePa November 12, 2008
Java help Needed!
Login in to Rate this Post:     0       ?        

 
Write a Java application that creates an instance of the String class and initializes this instance with a String literal. Use a for loop structure to print the string in reverse order. Implement the following two String member methods.


length( )
charAt( )

 Write a program to test class MyStringTest.

Here is my solution. But how else will you test MyStringaTest???? Somebody wants three different ways to do it??? Guys help please???!! Anyone?

  Solution:

public class StringTest {
public static void main(String args[]) {   
      String name= "sajha";
      int len = name.length();
      String s="";
     
          for(int i=len-1; i>=0; i--) {   
         
           //Get the character at that index
             char c=name.charAt(i); 
        
            //convert that char to string
              String s1 = Character.toString(c); 
        
             //concatenage each returned string till the loop is completed
               s=s.concat(s1);           
     }     
        
       System.out.println (s);      
      
  }
}

Read Full Discussion Thread for this article