0% found this document useful (0 votes)
13 views

String Programs

The document describes programs to check if a string is a palindrome, convert words to Pig Latin, count words in a string, check for unique characters, get the first letter of each word, find character ASCII values, and find the longest/shortest words. It provides the source code for each program and examples of its output.

Uploaded by

Neeil Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

String Programs

The document describes programs to check if a string is a palindrome, convert words to Pig Latin, count words in a string, check for unique characters, get the first letter of each word, find character ASCII values, and find the longest/shortest words. It provides the source code for each program and examples of its output.

Uploaded by

Neeil Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

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 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

Enter string: Programs in Java

Total number of words: 3


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

Enter a word: computer

computer is a Unique word


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

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 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

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 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

Longest word: extraordinary Length: 13


Shortest word: an Length: 2
Program to print the longest and shortest word in a sentence

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

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 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");
}
}

You might also like