[Go to site: main page, start]

0% found this document useful (0 votes)
3 views2 pages

Java String Reversal and Palindrome Check

Uploaded by

y5woff
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java String Reversal and Palindrome Check

Uploaded by

y5woff
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Answer java lab string

Exercise 1 :
import [Link].*;

class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner([Link]);

[Link]("Enter a string to reverse");


original = [Link]();

int length = [Link]();

for ( int i = length - 1 ; i >= 0 ; i-- )


reverse = reverse + [Link](i);

[Link]("Reverse of entered string is: "+reverse);


}
}

Exercise 2:
Method 1
import [Link].*;

class Palindrome
{
public static void main(String args[])
{
String original, reverse = ""; // Objects of String class
Scanner in = new Scanner([Link]);

[Link]("Enter a string to check if it is a palindrome");


original = [Link]();

int length = [Link]();

for ( int i = length - 1; i >= 0; i-- )


reverse = reverse + [Link](i);

if ([Link](reverse))
[Link]("Entered string is a palindrome.");
else
[Link]("Entered string is not a palindrome.");

}
}

Method 2
import [Link].*;

class Palindrome
{
public static void main(String args[])
{
String inputString;
Scanner in = new Scanner([Link]);

[Link]("Input a string");
inputString = [Link]();

int length = [Link]();


int i, begin, end, middle;

begin = 0;
end = length - 1;
middle = (begin + end)/2;

for (i = begin; i <= middle; i++) {


if ([Link](begin) == [Link](end)) {
begin++;
end--;
}
else {
break;
}
}
if (i == middle + 1) {
[Link]("Palindrome");
}
else {
[Link]("Not a palindrome");
}
}
}

You might also like