String Programs
String Programs
racecar is palindrome
Program to check whether a string is Palindrome or not
Source code:
import java.util.*;
class Palindrome
{
public static void main()
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("\tProgram to check whether a string is Palindrome or not");
System.out.print("\nEnter a word to check:");
original = in.next();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
{
reverse = reverse + original.charAt(i);
}
if (original.equals(reverse))
System.out.println("\n"+original+" is palindrome");
else
System.out.println("\n"+original+" is not palindrome");
}
}
OUTPUT
Program to convert a word into a PigLatin word
Enter a word:TROUBLE
PigLatin word:OUBLETRAY
Program to convert a word into PigLatin word
Source code:
import java.util.Scanner;
class Piglatin
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("\tProgram to convert a word into a PigLatin word");
System.out.print("\nEnter a word:");
String n = in.next();
int p = 0,i;
for(i = 0;i<n.length();i++)
{
char c = n.charAt(i);
if(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
break;
}
if(i==0)
System.out.println (n);
else
System.out.println ("\nPigLatin word:" + n.substring(i) + n.substring(0,i) +"AY");
}
}
OUTPUT
Program to count the number of words present in a string
Source code:
import java.util.*;
class Count_Words
{
public static void main()
{
String text;
int countWords=0;
Scanner sc=new Scanner(System.in);
System.out.println("\tProgram to count the number of words present in a string");
System.out.print("\nEnter string:");
text=sc.nextLine();
//word count
for(int i=0; i<text.length()-1; i++)
{
if(text.charAt(i)==' ' && text.charAt(i+1)!=' ')
countWords++;
}
System.out.println("\nTotal number of words:"+ (countWords+1));
}
}
OUTPUT
Program to check whether a string is unique or not
Source code:
import java.util.Scanner;
class Unique
{
public static void main()
{
System.out.println("\t Program to check whether a string is unique or not");
Scanner in = new Scanner(System.in);
System.out.print("\nEnter a word:");
String s = in.next();
int flag=0;
char ch;
for(int i=0;i<s.length();i++)
{
ch=s.charAt(i);
if(s.indexOf(ch)!=s.lastIndexOf(ch))
{
flag=1;
break;
}
}
if(flag==0)
System.out.println("\n"+s+" is a Unique word");
else
System.out.println("\n"+s+" is not a Unique word");
}
}
OUTPUT
Program to print the first letter of each word in the string
Source code:
import java.util.Scanner;
class New_Word
{
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("\tProgram to print the first letter of each word in the string");
System.out.print("\nEnter any string:");
String str=in.nextLine();
str=" "+str;
String first="";
System.out.print("\nNew Word:");
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if(ch==' ')
{
System.out.print(str.charAt(i+1));
}
}
}
}
OUTPUT
Source code:
import java.util.*;
class ASCII
{
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("\tProgram to find the ASCII value of each character in a string");
System.out.print("\nEnter a word:");
String s=in.next();
for(int i=0;i<s.length();i++)
{
System.out.println("ASCII value of "+(s.charAt(i))+" = (int)(s.charAt(i)));;
}
}
}
OUTPUT
Program to print the longest and shortest word in a sentence
Enter any sentence: Hardships often prepare ordinary people for an extraordinary destiny
Source code:
import java.util.*;
class Long_Short
{
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("\tProgram to print the longest and shortest word in a sentence");
System.out.print("\nEnter any sentence:");
String s=in.nextLine();
s=s+" ";
String max="",min=s,w="";
int index=0;
while(index<s.length())
{
int position=s.indexOf(' ',index);
w=s.substring(index,position);
if(w.length()>max.length())
max=w;
if(w.length()<min.length())
min=w;
index=position+1;
w="";
}
System.out.println("\nLongest word:"+max+" Length:"+max.length());
System.out.println("Shortest word:"+min+" Length:"+min.length());
}
}
OUTPUT
Program to search for a string in an array
Source code:
import java.util.Scanner;
class LinearSearch_String
{
public static void main()
{
Scanner in = new Scanner(System.in);
String arr[]=new String[5];
System.out.println("\tProgram to search for a string in an array");
for(int i=0;i<5;i++)
{
System.out.print("\nEnter the word at position "+(i+1)+":");
arr[i]=in.next();
}
System.out.print("\nEnter the word to be searched:");
String search=in.next();
int flag=0;
for(int i=0;i<5;i++)
{
if(arr[i].equalsIgnoreCase(search))
{
System.out.println("\nThe word is found at the position:"+(i+1));
flag=1;
break;
}
}
if(flag==0)
System.out.println("\nWord is not found");
}
}