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

String programs computer

The document contains multiple Java programs that perform various string manipulations, including finding the longest and shortest words, displaying first and last characters of words, checking for unique strings, palindrome checks, ASCII code display, vowel frequency, and more. Each program includes sample input and output to illustrate its functionality. The programs demonstrate fundamental string handling techniques in Java.

Uploaded by

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

String programs computer

The document contains multiple Java programs that perform various string manipulations, including finding the longest and shortest words, displaying first and last characters of words, checking for unique strings, palindrome checks, ASCII code display, vowel frequency, and more. Each program includes sample input and output to illustrate its functionality. The programs demonstrate fundamental string handling techniques in Java.

Uploaded by

routhushree
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

/*

1. Write a program in Java to enter a String/Sentence and display the longest word and the length of the
longest word present in the String.
Sample Input: “TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BAGAN”
Sample Output: The longest word: FOOTBALL: The length of the word: 8*/
import java.util.*;
public class longest_word
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter string");
String st=s.nextLine();
st=st+" ";
int len=st.length();
int max_len=0,word_len=0;
String max_word="";
String word="";
char ch;
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
if(ch!=' ')
{
word=word+ch;
}
else
{
word_len=word.length();

if(word_len>max_len)
{
max_len=word_len;
max_word=word;
}
word="";
}
}
System.out.println("Longest word="+ max_word);
System.out.println("Length of the longest word="+max_len);

}
}
/*
2. Write a program in Java to enter a String/Sentence and display the shortest word and the length of the
longest word present in the String.
Sample Input: “TATA FOOTBALL ACADEMY”
Sample Output: The shortest word: TATA: The length of the word: 3*/
import java.util.*;
public class shortest_word
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter string");
String st=s.nextLine();
st=st+" ";
int len=st.length();
int min_len=len,word_len=0;
String min_word="";
String word="";
char ch;
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
if(ch!=' ')
{
word=word+ch;
}
else
{
word_len=word.length();

if(word_len<min_len)
{
min_len=word_len;
min_word=word;
}
word="";
}
}
System.out.println("Shortest word="+ min_word);
System.out.println("Length of the shortest word="+min_len);

}
}
3. Write a program in Java to enter a String and display first character of each word
import java.util.Scanner;
public class first_letter
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter the string ");
String st=s.nextLine();
st=st.toUpperCase();
st=" "+st;
char ch;
String st1="";
for(int i=0;i<st.length();i++)
{
ch=st.charAt(i);
if(ch==' ')
st1=st1+(st.charAt(++i));

}
System.out.println("original string "+st);
System.out.println("original string "+st1);
}
}
4. Write a program in Java to enter a String and display last character of each word

import java.util.Scanner;
public class last_letter
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter the string ");
String st=s.nextLine();
st=st.toUpperCase();
st=st+" ";
char ch;
String st1="";
for(int i=0;i<st.length();i++)
{
ch=st.charAt(i);
if(ch==' ')
st1=st1+(st.charAt(i-1));

}
System.out.println("original string "+st);
System.out.println("original string "+st1);
}
}

/*5 . A string is said to be ‘Unique’ if none of the letters present in the string are repeated.
Write a program to accept a string and check whether the string is Unique or not. The program displays a
message accordingly.
Sample Input: COMPUTER
Sample Output: Unique String*/

import java.util.*;
public class unique_string
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter string");
String st=s.next();
int len=st.length();
char ch1,ch2;
int temp=0;
st=st.toUpperCase();
for(int i=0;i<len;i++)
{
ch1=st.charAt(i);
for(int j=i+1;j<len;j++)
{
ch2=st.charAt(j);
if(ch1==ch2)
{
temp=1;
break;
}
}
}
if(temp==1)
System.out.println(st+" is not a unique string");
else
System.out.println(st+" is a unique string");
}
}

/*6. Write a program in Java to enter word and check whether the word is a palindrome word or not*/
import java.util.*;
public class palindrome
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter string");
String st=s.nextLine();
int len=st.length();
String rev="";
char ch;
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
rev=ch+rev;
}
if(st.equalsIgnoreCase(rev))
System.out.println(st+"is a palindrome");
else
System.out.println(st+"is not a palindrome");
}}
/*7. write a program that accept a word,convert word into uppercase and check if it is palindrome or just a
special
word .
A palindrome word is a word which reads the same backward and forward .
Example: MADAM ,LEVEL etc.
Special words are words which starts and ends with same character
Example : COMIC, ASIA*/
import java.util.*;
public class palindrome_special
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter string");
String st = sc.nextLine();
int len=st.length();
st=st.toUpperCase();
char ch;String rev="";
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
rev=ch+rev;
}
char ch1=st.charAt(0);
char ch2=st.charAt(len-1);
if(st.equals(rev))
System.out.println(st+" is a palindrome");
else if(ch1==ch2)
System.out.println(st+" is a special word");
else
System.out.println(st+" is neither special nor palindrome");
}
}
/*8. write a program to convert a non palindrome word into a palindrom word just by adding word and
reversed
word together
example : computer
output: computerretupmoc*/
import java.util.*;
public class nonpalin_palindrome
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter string");
String st = sc.nextLine();
int len=st.length();
char ch;String rev="";
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
rev=ch+rev;
}
System.out.println("word="+st);
System.out.println("palindromeword="+(st+rev));
}
}

/*9. Write a program in Java to accept a word and display the ASCII code of each character of the word.
Sample Input: BLUEJ
Sample Output:
ASCII of B = 66
ASCII of L = 76
ASCII of U = 85
ASCII of E = 69
ASCII of J = 74*/
import java.util.*;
public class ascii_code
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter string");
String st=s.next();
int len=st.length();
char ch;
System.out.println("Character"+"\t"+"Ascii code");
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
System.out.println(ch+ "\t" + (int)ch);
}
}
}
10. Write aprogram in java to input string in mixed case .find and display frequency of each vowel
// frequency of each vowel
import java.util.*;
public class Frequency_vowels
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter the string ");
String str=s.nextLine();
char ch;
int a=0,e=0,i=0,o=0,u=0;
for(int j=0;j<str.length();j++)
{
ch=str.charAt(j);

if(ch=='A' || ch=='a')
a++;
else if(ch=='E' || ch=='e')
e++;
else if(ch=='I' || ch=='i')
i++;
else if(ch=='O' || ch=='o')//1
o++;
else if(ch=='U' || ch=='u')
u++;

}
System.out.println("frequency of a is"+a);
System.out.println("frequency of e is"+e);
System.out.println("frequency of i is"+i);
System.out.println("frequency of o is"+o);
System.out.println("frequency of u is"+u);
}
}
11. Write a program in java to accept a sentence in a mixed case.replace all the vowels with’*’ sign and display
sentence
import java.util.*;
public class add_asterik_vowel {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter the string");
String st=s.nextLine();
String st1="";
int len=st.length();
char ch;
for(int i=0;i<len;i++)//computer
{
ch=st.charAt(i);//c
if(ch=='a' || ch=='A'|| ch=='e' || ch=='E'|| ch=='i' || ch=='I'||ch=='o' || ch=='O'||ch=='u' || ch=='U')
{
ch='*';
}
st1=st1+ch;//c
}
System.out.println("the original string is"+st);
System.out.println("modified String is"+st1);
}
}
/*12. Write a program in java to accept a word and encrypt as follows:
eg. word encrypted form
Zeal xfbj
program nppepbk
anode blpbf*/
import java.util.*;
public class encrypt
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter string");
String st=s.next();
int len=st.length();
char ch;
String st1="";
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
if(Character.isLetter(ch))
{
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
ch=(char)(ch+1);
else
ch=(char)(ch-2);
}

st1=st1+ch;
}
System.out.println("Original word="+st);
System.out.println("Encrypted word="+st1);
}
}

/*13. Write a program in Java to accept a 3-letter word. Display all the probable 3 letter combinations such
that no letter should be repeated in the output within each combination.
Sample Input:
CAT
Sample Output:
CAT ,CTA,TAC,TCA,ACT,ATC*/

import java.util.*;
public class probable_combination
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter the string");
String st=s.next();
int len=st.length();
if(len==3)
{
for(int i=0;i<len;i++)
{
for(int j=0;j<len;j++)
{
for(int k=0;k<len;k++)
{
if(i!=j && j!=k && k!=i)

{
System.out.println(st.charAt(i)+""+st.charAt(j)+""+ st.charAt(k));
}
}
}
}
}

else
System.out.println("enter valid 3 letter word");
}
}

You might also like