Advertisement
cgorrillaha

traverse strings

Nov 13th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. public class StringTraversal {
  2.     public static void main(String[] args) {
  3.         String s="Hello world!";
  4.         for(int i=0; i<s.length(); i++){
  5.             String ch=s.substring(i,i+1);
  6.             System.out.print(ch);
  7.         }
  8.         System.out.println("\nDONE!");
  9.  
  10.         for(int i=s.length()-1; i>=0; i--){
  11.             String ch=s.substring(i,i+1);
  12.             System.out.print(ch);
  13.         }
  14.         System.out.println("\nDONE!");
  15.  
  16.         for(int i=0; i<s.length(); i++){
  17.             String ch=s.substring(s.length()-1-i,s.length()-i);
  18.             System.out.print(ch);
  19.         }
  20.         System.out.println("\nDONE!");
  21.     }
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement