# String in java is a class!! String objects are immutable which means once created cannot be destroyed. There are various way of instantiating or creating objects of string.
1. String s1="ABC";
2. String s2=new String("ABC");
3. char [] abc={'A','B','C'};
String s3=new String(abc);
So the problem statement is to reverse a string. Input="ABCFAD" Outpt=DAFCBA. Below is the code snippet to achieve the above problem statement.
class StringReversing
{
public void computeStringReversal(String string){
// convert the string into character of array
char [] characterArray=string.toCharArray();
int lowerIndex=0;
int higherIndex=string.length()-1;
char temp;
while(higherIndex>lowerIndex)
{
temp=characterArray[lowerIndex];
characterArray[lowerIndex]=characterArray[higherIndex];
characterArray[higherIndex]=temp;
higherIndex--;
lowerIndex++;
}
System.out.println(new String(characterArray));
}
}
public class TestStringReversing {
public static void main(String[] args) {
StringReversing stringReversing=new StringReversing();
stringReversing.computeStringReversal("ABCFAD");
}
}
1. String s1="ABC";
2. String s2=new String("ABC");
3. char [] abc={'A','B','C'};
String s3=new String(abc);
So the problem statement is to reverse a string. Input="ABCFAD" Outpt=DAFCBA. Below is the code snippet to achieve the above problem statement.
class StringReversing
{
public void computeStringReversal(String string){
// convert the string into character of array
char [] characterArray=string.toCharArray();
int lowerIndex=0;
int higherIndex=string.length()-1;
char temp;
while(higherIndex>lowerIndex)
{
temp=characterArray[lowerIndex];
characterArray[lowerIndex]=characterArray[higherIndex];
characterArray[higherIndex]=temp;
higherIndex--;
lowerIndex++;
}
System.out.println(new String(characterArray));
}
}
public class TestStringReversing {
public static void main(String[] args) {
StringReversing stringReversing=new StringReversing();
stringReversing.computeStringReversal("ABCFAD");
}
}
Comments
Post a Comment