OUTPUT
Program to check whether a string is Palindrome or not
Enter a word to check: racecar
racecar is palindrome
Program to check whether a string is Palindrome or not
Source code:
import [Link].*;
class Palindrome
{
public static void main()
{
String original, reverse = "";
Scanner in = new Scanner([Link]);
[Link]("\tProgram to check whether a string is Palindrome or not");
[Link]("\nEnter a word to check:");
original = [Link]();
int length = [Link]();
for ( int i = length - 1; i >= 0; i-- )
{
reverse = reverse + [Link](i);
}
if ([Link](reverse))
[Link]("\n"+original+" is palindrome");
else
[Link]("\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 [Link];
class Piglatin
{
public static void main()
{
Scanner in = new Scanner([Link]);
[Link]("\tProgram to convert a word into a PigLatin word");
[Link]("\nEnter a word:");
String n = [Link]();
int p = 0,i;
for(i = 0;i<[Link]();i++)
{
char c = [Link](i);
if(c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
break;
}
if(i==0)
[Link] (n);
else
[Link] ("\nPigLatin word:" + [Link](i) + [Link](0,i) +"AY");
}
}
OUTPUT
Program to count the number of words present in a string
Enter string: Programs in Java
Total number of words: 3
Program to count the number of words present in a string
Source code:
import [Link].*;
class Count_Words
{
public static void main()
{
String text;
int countWords=0;
Scanner sc=new Scanner([Link]);
[Link]("\tProgram to count the number of words present in a string");
[Link]("\nEnter string:");
text=[Link]();
//word count
for(int i=0; i<[Link]()-1; i++)
{
if([Link](i)==' ' && [Link](i+1)!=' ')
countWords++;
}
[Link]("\nTotal number of words:"+ (countWords+1));
}
}
OUTPUT
Program to check whether a string is unique or not
Enter a word: computer
computer is a Unique word
Program to check whether a string is unique or not
Source code:
import [Link];
class Unique
{
public static void main()
{
[Link]("\t Program to check whether a string is unique or not");
Scanner in = new Scanner([Link]);
[Link]("\nEnter a word:");
String s = [Link]();
int flag=0;
char ch;
for(int i=0;i<[Link]();i++)
{
ch=[Link](i);
if([Link](ch)!=[Link](ch))
{
flag=1;
break;
}
}
if(flag==0)
[Link]("\n"+s+" is a Unique word");
else
[Link]("\n"+s+" is not a Unique word");
}
}
OUTPUT
Program to print the first letter of each word in the string
Enter any string: Vital Information Resource Under Seize
New Word: VIRUS
Program to print the form a new word by joining the first letter of each word in the
string
Source code:
import [Link];
class New_Word
{
public static void main()
{
Scanner in=new Scanner([Link]);
[Link]("\tProgram to print the first letter of each word in the string");
[Link]("\nEnter any string:");
String str=[Link]();
str=" "+str;
String first="";
[Link]("\nNew Word:");
for(int i=0;i<[Link]();i++)
{
char ch=[Link](i);
if(ch==' ')
{
[Link]([Link](i+1));
}
}
}
}
OUTPUT
Program to find the ASCII value of each character in a string
Enter a word: BLUEJ
ASCII value of B = 66
ASCII value of L = 76
ASCII value of U = 85
ASCII value of E = 69
ASCII value of J = 74
Program to print the ASCII value of each character in a string
Source code:
import [Link].*;
class ASCII
{
public static void main()
{
Scanner in=new Scanner([Link]);
[Link]("\tProgram to find the ASCII value of each character in a string");
[Link]("\nEnter a word:");
String s=[Link]();
for(int i=0;i<[Link]();i++)
{
[Link]("ASCII value of "+([Link](i))+" = (int)([Link](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
Longest word: extraordinary Length: 13
Shortest word: an Length: 2
Program to print the longest and shortest word in a sentence
Source code:
import [Link].*;
class Long_Short
{
public static void main()
{
Scanner in=new Scanner([Link]);
[Link]("\tProgram to print the longest and shortest word in a sentence");
[Link]("\nEnter any sentence:");
String s=[Link]();
s=s+" ";
String max="",min=s,w="";
int index=0;
while(index<[Link]())
{
int position=[Link](' ',index);
w=[Link](index,position);
if([Link]()>[Link]())
max=w;
if([Link]()<[Link]())
min=w;
index=position+1;
w="";
}
[Link]("\nLongest word:"+max+" Length:"+[Link]());
[Link]("Shortest word:"+min+" Length:"+[Link]());
}
}
OUTPUT
Program to search for a string in an array
Enter the word at position 1: square
Enter the word at position 2: rectangle
Enter the word at position 3: triangle
Enter the word at position 4: circle
Enter the word at position 5: sphere
Enter the word to be searched: triangle
The word is found at the position: 3
Program to search for a string in an array
Source code:
import [Link];
class LinearSearch_String
{
public static void main()
{
Scanner in = new Scanner([Link]);
String arr[]=new String[5];
[Link]("\tProgram to search for a string in an array");
for(int i=0;i<5;i++)
{
[Link]("\nEnter the word at position "+(i+1)+":");
arr[i]=[Link]();
}
[Link]("\nEnter the word to be searched:");
String search=[Link]();
int flag=0;
for(int i=0;i<5;i++)
{
if(arr[i].equalsIgnoreCase(search))
{
[Link]("\nThe word is found at the position:"+(i+1));
flag=1;
break;
}
}
if(flag==0)
[Link]("\nWord is not found");
}
}