Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java String Programs

Reverse Example

//Develop a Java program to find the reverse of a string value.
import java.util.*;
class ReverseExample
{
	public static void main(String[] args)
	{
		String str, reverse="";
		int length;

		Scanner s = new Scanner(System.in);
		System.out.println("Enter the string value");
		str=s.nextLine();

		length=str.length();
		for(int k=length-1;k>=0;k--)
		{
			reverse=reverse+str.charAt(k);
		}

		System.out.println("the reverse is"+" "+reverse);
	}
}
Output
img


Palindrome Example

//Develop a Java program to check whether a given string value is palindrome or not.
import java.util.*;
class PalindromeExample
{
	public static void main(String[] args)
	{
		String str, reverse="";
		int length;

		Scanner s = new Scanner(System.in);
		System.out.println("Enter the string value");
		str=s.nextLine();

		length=str.length();
		for(int k=length-1;k>=0;k--)
		{
			reverse=reverse+str.charAt(k);
		}

		System.out.println("the reverse is"+" "+reverse);
		if(str.equals(reverse))
		{
			System.out.println("string is palindrome");
		}
		else
		{
			System.out.println("string is not palindrome");
		}
	}
}
Output
img


Java Program to check whether a given string is palindrome or not without taking the concept of reverse.

import java.util.*;
class StringPalindrome {

	public static void main(String[] args) {
		String str;
		int l, c = 0;
		Scanner s = new Scanner(System.in);
		System.out.println("enter a string");
		str = s.nextLine();
		l = str.length();
		for (int i = 0; i < (l - 1) / 2; i++) {
			if (str.charAt(i) == str.charAt(l - 1 - i)) {
				continue;
			} else {
				c++;
			}
		}
	if (c == 0)
		System.out.println("the given string is a palindrome");
	else
		System.out.println("the given string is a not palindrome");
}

Output

pallindrome_program

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext