Strings are a sequence of characters used in programming. In the Java programming language, strings are defined as objects.
The Java platform provides the String class to create and manipulate strings.
Creating Strings:
The most common way to create a string is to write:
String greeting = " ";
or String name ="deepak";
String Length:
Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object
The following program illustrate the use of String using String Functions. This program make use of following functions:
1. int compareTo(String anotherString) : Compares two strings lexicographically.
2.int (string)length() :Returns the number of characters contained in the string object
Checking whether the input String is Palindrome or not...
import java.io.*;
import java.util.Scanner;
class Hello{
public static void main(String args[])throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String ");
String s=sc.next();
String p="";
int m=s.length();
for(int i=m-1;i>=0;i--) {
char c = s.charAt(i);
p= p + c;
}
if(s.compareTo(p)==0)
{
System.out.println("Its palindrome");
}
else
System.out.println("Not a palindrome");
}
}
Output:
Enter the String :Madam
Its is Palindrome
0 Comment(s)