Board String Imp Practice Program
Board String Imp Practice Program
/*A class Encode has been defined to input a string ,convert the string in uppercase and replace only
* the vowels in a word by the next corresponding vowel and
forms a new word. i.e. A → E, E → I, I → O, O → U and U → A
Example:
Input: COMPUTER
Output: CUMPATIR*/
import java.util.*;
class Encode
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the word:");
String st= sc.next();
st=st.toUpperCase();
String st1="";
int len=st.length();
for(int i=0;i< len;i++)
{
char ch=st.charAt(i);
if(ch=='A')
ch='E';
else if(ch=='E')
ch='I';
else if(ch=='I')
ch='O';
else if(ch=='O')
ch='U';
else if(ch=='U')
ch='A';
else
ch=ch;
st1=st1+ch;
}
System.out.println("Original String="+st);
System.out.println("Encoded String="+st1);
}
}Question 2
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each
vowel by immediate next character and every consonant by the previous characters remain the same.
Example : input : #IMAGINTION@2024
Output:#JLBFJMBSJPM@2024
import java.util.*;
class convert
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter string");
String st=sc.nextLine();
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')
st1=st1+(char)(ch+1);
else
st1=st1+(char)(ch-1);
}
else
st1=st1+ch;
}
System.out.println("Input="+st);
System.out.println("output="+st1);
}
}
Question 3Define a class to accept a String and print the number of digits, alphabets and special characters in the string.
Example:
S = "KAPILDEV@83"
Output:
Number of digits – 2
Number of Alphabets – 8
Number of Special characters – 1
import java.util.Scanner;
public class frequency
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String st= sc.nextLine();
int len = st.length();
int alpha = 0,dig = 0,sp = 0;
char ch;
Question 5 Write a program to enter a String ,convert the string in upper case 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+1));
}
System.out.println("string = "+st);
System.out.println("first character of each word "+st1);
}
}
Question 6 Write a program to enter a String ,convert string into uppercase 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("string "+st);
System.out.println("Last character of each word "+st1);
}
}
Question 7/*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.
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");
}
}
Question 8/* . Sam designs a program to check the strength of a password. A strong password should
satisfy the following conditions:
→length of the password should be atleast 12 characters
→should atleast have 4 upper case letters, 4 lower case letters, 2 digits, 2 special
characters
Define a class accept the password and check whether the password is strong or not.*/
import java.util.*;
class strength
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter password");
String pw=sc.next();
int len=pw.length();
char ch, uc=0,lc=0,d=0,sp=0;
for(int i=0;i<len;i++)
{
ch=pw.charAt(i);
if(Character.isUpperCase(ch))
uc++;
if(Character.isLowerCase(ch))
lc++;
if(Character.isDigit(ch))
d++;
if(!Character.isLetterOrDigit(ch))
sp++;
}
if (uc<4|| lc<4|| d<2 || sp<2 || len<12)
System.out.println(pw+"is a strong password");
else
System.out.println(pw+"is not a strong password");
}
}
Question 9/* 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");
}
}
Question 10. 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);
}
}