Java String Program Board Questions


  1. Define a class to accept the gmail id and check for its validity. [15 MARKS - 2024]
    A gmail id is valid only if it has:

    @
    .(dot)
    gmail
    com
    
    Example: icse2024@gmail.com is a valid gmail id
  2. import java.util.*;
    
    public class Prac
    {
    	public static void main(String args[])
    	{
    		Scanner sc=new Scanner(System.in);
    		
    		System.out.println("Enter gmail id : ");
    		String s=sc.next();
    		
    		if(s.endsWith("@gmail.com"))
    			System.out.println("Valid");
    		else
    			System.out.println("Not Valid");
    			
    	}
    }

  3. Define a class to accept a String and print the number of digits, alphabets, spaces and special characters in the string. [15 MARKS - 2023]

    Example : 
    S = "KAPILDEV@83"
    
    Output : 
    Number of Digits - 2
    Number of Alphabets - 8
    Number of Spaces - 0
    Number of Special Characters - 1
    
  4. import java.util.*;
    
    public class Prac
    {
    	public static void main(String args[])
    	{
    		Scanner sc=new Scanner(System.in);
    		
    		System.out.println("Enter a string :");
    		String s=sc.nextLine();
    		
    		int digit=0, letter=0, space=0, special=0;
    		
    		for(int i=0; i<s.length(); i++) {
    			char ch=s.charAt(i);
    			
    			if(Character.isDigit(ch))
    				digit = digit + 1;
    			
    			else if(Character.isLetter(ch))
    				letter = letter + 1;
    			
    			else if(Character.isWhitespace(ch))
    				space = space + 1;
    			
    			else
    				special = special + 1;
    			
    		}
    		
    		System.out.println("Number of Alphabets : " +letter);
    		System.out.println("Number of Digits : " +digit);
    		System.out.println("Number of Spaces : " +space);
    		System.out.println("Number of Special Characters : " +special);
    		
    	}
    }
    

  5. Define a class to accept a string, and print the characters with the uppercase and lowercase reversed, but all the other characters should remain the same as before. [15 MARKS - 2022]

    Example : 
    INPUT : 
    WelCoMe_2022
    
    OUTPUT : 
    wELcOmE_2022
    
  6. import java.util.*;
    
    public class Prac
    {
    	public static void main(String args[])
    	{
    		Scanner sc=new Scanner(System.in);
    		
    		System.out.println("Enter a string :");
    		String s1=sc.nextLine();
    		
    		String s2="";
    		
    		for(int i=0; i<s1.length(); i++) {
    			char ch=s1.charAt(i);
    			
    			if(Character.isUpperCase(ch))
    				ch=Character.toLowerCase(ch);
    			
    			else if(Character.isLowerCase(ch))
    				ch=Character.toUpperCase(ch);
    			
    			s2=s2+ch;			
    			
    		}
    		
    		System.out.println("Original String is : "+s1);
    		System.out.println("New String is : "+s2);
    				
    	}
    }
    

  7. Write a program to input a sentence and convert it into uppercase and display each word in a separate line. [15 MARKS - 2020]

    Example : 
    INPUT : 
    India is my country
    
    OUTPUT : 
    INDIA
    IS
    MY
    COUNTRY
    
  8. import java.util.*;
    
    public class Prac
    {
    	public static void main(String args[])
    	{
    		Scanner sc=new Scanner(System.in);
    		
    		System.out.println("Enter a string :");
    		String s1=sc.nextLine();
    		s1=s1+' ';
    		
    		String word="";
    		
    		for(int i=0; i<s1.length(); i++) {
    			char ch=s1.charAt(i);
    			
    			if(ch==' ') {
    				System.out.println(word);
    				word="";
    			}
    			else {
    				word=word+ch;
    			}
    			
    		}
    			
    	}
    }
    

  9. Write a program to input a sentence and convert it into uppercase and count and display the total number of words starting with a letter ‘A’. [15 MARKS - 2019]

    Example :
    INPUT : 
    ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING
    
    OUTPUT : 
    Total number of words starting with letter ‘A’ = 4
    
  10. import java.util.*;
    
    public class Prac
    {
    	public static void main(String args[])
    	{
    		Scanner sc=new Scanner(System.in);
    		
    		System.out.println("Enter a string :");
    		String s1=sc.nextLine();
    		
    		s1=s1.toUpperCase()+' ';
    		
    		String word="";
    		int count=0;
    		
    		for(int i=0; i<s1.length(); i++) {
    			char ch=s1.charAt(i);
    			
    			if(ch==' ') {
    				if(word.charAt(0)=='A') {
    					count++;
    				}
    				word="";
    			}
    			else {
    				word=word+ch;
    			}
    			
    		}
    		
    		System.out.println("Total number of words starting with 'A' : "+count);
    			
    	}
    }
    

  11. Write a program in Java to accept a string in lowercase and change the first letter of every word to uppercase. Display the new string. [15 MARKS - 2018]

    Example : 
    INPUT : 
    we are in cyber world
    
    OUTPUT : 
    We Are In Cyber World
    
  12. import java.util.*;
    
    public class Prac
    {
    	public static void main(String args[])
    	{
    		Scanner sc=new Scanner(System.in);
    		
    		System.out.println("Enter a string :");
    		String s1=sc.nextLine();
    		
    		s1=' '+s1.toLowerCase();
    		
    		String s2="";
    		
    		for(int i=0; i<s1.length(); i++) {
    			char ch=s1.charAt(i);
    			
    			if(ch==' ') {
    				ch=s1.charAt(i+1);
    				ch=Character.toUpperCase(ch);
    				s2=s2+' ';
    				i=i+1;
    			}
    			
    			s2=s2+ch;
    			
    		}
    		
    		System.out.println("New string : "+s2.trim());
    			
    	}
    }
    

  13. Write a program to accept a word, check and print whether the word is a palindrome or only special word. [15 MARKS - 2016]

    Special words are those words which start and end with the same letter.
    Example : 
    EXISTENCE, COMIC, WINDOW
    
    Palindrome words are those words which read the from left to right and vice-versa.
    Example : 
    MALAYALAM, MADAM, LEVEL, ROTATOR, CIVIC
    
    All palindromes are special words, but all special words are not palindrome.
  14. import java.util.*;
    
    public class Prac
    {
    	public static void main(String args[])
    	{
    		Scanner sc=new Scanner(System.in);
    		
    		System.out.println("Enter a string :");
    		String s1=sc.next();
    		s1=s1.toUpperCase();
    		
    		int len=s1.length();
    				
    		String rev="";
    		
    		for(int i=0; i<s1.length(); i++) {
    			char ch=s1.charAt(i);
    			
    			rev=ch+rev;
    			
    		}
    		
    		if(s1.equals(rev))
    			System.out.println("Palindrome String");
    		
    		else if(s1.charAt(0) == s1.charAt(len-1))
    			System.out.println("Special Word");
    		
    		else
    			System.out.println("Neither Palindrome Nor Special");
    			
    	}
    }
    

  15. Write a program to assign a full path and file name as given below. Using library functions, extract and output the file path, file name and file extension separately as shown. [15 MARKS - 2014]

    Example : 
    INPUT : 
    C:\Users\admin\Pictures\flower.jpg
    
    OUTPUT : 
    Path : C:\Users\admin\Pictures\
    File name : flower
    Extension : jpg
    
  16. COMING SOON

  17. Write a program that encodes a word into Piglatin, To translate word into a Piglatin word, convert the word into uppercase and then place the first vowel of the original word as the start of the new word along with the remaining alphabets. The alphabets present before the vowel being shifted towards the end followed by “AY”. [15 MARKS - 2013]

    Example : 
    INPUT(1) : London
    OUTPUT(1)  : ONDONLAY
    
    INPUT(2) : Olympics
    OUTPUT(2) : OLYMPICSAY
    
  18. COMING SOON

  19. Write a program to accept a string. Convert the string to uppercase. Count and output the number of double letter sequences that exist in the string. [15 MARKS - 2012]

    Example : 
    INPUT : 
    SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
    
    OUTPUT  : 
    Number of double letter sequences = 4
    
  20. COMING SOON

  21. Write a program to accept a word and convert it into lower case, if it is in upper case. Display the new word by replacing only the vowels with the letter following it. [15 MARKS - 2011]

    Sample Input: 
    computer
    
    Sample Output: 
    cpmpvtfr
    
  22. COMING SOON

  23. Write a program to input a sentence. Count and display the frequency of each letter of the sentence in alphabetical order. [15 MARKS - 2010]

     Sample Input: APPLICATIONS Sample Output: Character - Frequency A - 2 P - 2 L - 1 C - 1 T - 1 I - 2 O - 1 N - 1 S - 1 
  24. COMING SOON