Sunday, January 26, 2014

Java Code

1. Reverse a String:

/**
 *
 */
package perform;

/**
 * @author Sai Saripalli
 *
 */
public class ReverseString {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String str= "abcedf";
int lastIndex = str.length() -1;
char[] cha= str.toCharArray();
//System.out.println(cha.length);

for(int i=0; i<=lastIndex/2; i++){
cha = swap(cha, i, lastIndex-i);
}

for(int i=0;i<cha.length;i++)
System.out.println(cha[i]);
}
 System.out.println(String.valueOf(cha));

public static char[] swap(char[] cha, int x, int y){
char z= cha[x];
cha[x]=cha[y];
cha[y]= z;
return cha;
}

}

No comments:

Post a Comment