File 1: StringRoutines.java
--------------------------------------------------------------
package org.developersbrain.solutions;
public class StringRoutines {
int getArrayLength(char[] arrList){
return arrList.length;
}
String convertArrString(char[] arrList){
int arrLength = getArrayLength(arrList);
String retString="";
for(int i=0;i<arrLength;i++){
retString=retString+arrList[i];
}
return retString;
}
public String reverse(String str){
char[] chrArray=str.toCharArray();
int arrLength=getArrayLength(chrArray);
char tmp;
for(int i=0,j=arrLength-1;i<arrLength/2;i++,j--){
tmp=chrArray[j];
chrArray[j]=chrArray[i];
chrArray[i]=tmp;
}
return convertArrString(chrArray);
}
}
--------------------------------------------------------------
package org.developersbrain.solutions;
public class StringRoutines {
int getArrayLength(char[] arrList){
return arrList.length;
}
String convertArrString(char[] arrList){
int arrLength = getArrayLength(arrList);
String retString="";
for(int i=0;i<arrLength;i++){
retString=retString+arrList[i];
}
return retString;
}
public String reverse(String str){
char[] chrArray=str.toCharArray();
int arrLength=getArrayLength(chrArray);
char tmp;
for(int i=0,j=arrLength-1;i<arrLength/2;i++,j--){
tmp=chrArray[j];
chrArray[j]=chrArray[i];
chrArray[i]=tmp;
}
return convertArrString(chrArray);
}
}
File2: MainClass.java
------------------------------------------------------------------
package org.developersbrain.solutions;
public class MainClass {
public static void main(String[] args){
StringRoutines sr=new StringRoutines();
String str="ABCDEFGHIJKLMNOP";
System.out.println("Given String:"+str);
System.out.println("Modifed String:"+sr.reverse(str));
}
}
Ouput:
--------------------------------------------------------------------------------
Given String:ABCDEFGHIJKLMNOP
Modifed String:PONMLKJIHGFEDCBA
Comments
Post a Comment