Sunday, 15 December 2024

CLASS - X : PROGRAM BASED ON STRING MANIPULATIONS

 PROGRAM BASED ON STRING MANIPULATIONS 

Question 1: Write a program in Java to accept a word and display the same in Pig Latin form. A word is framed in Pig Lati form by using the following steps:

i. Enter a Word

ii. Shift all letters which are available before the first vowel, towards the end of the word.

iii. Finally, add 'AY' at the end of the word sequence.                             [ICSE 2013]

Sample Input: TROUBLE

Sample Output: OUBLETRAY

Solution:-  

/// display a Pig Latin Word

public class ConvertPigLatin

{

      public static void main(String args[])

      {

        int x,y;

        String str1,str2;

        char b; b=0;

        x=str.length(); 

        System.out.println("The Pig Latin form of the given string");

        for(y=0;y<x;y++)     

        {

             b=(str.charAt(y));

             if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u'||b=='A'||b=='E'||b=='I'||b=='O'||b=='U')

             break;

        } 

str1=str.substring(y,x);

str2=str.substring(0,y);

        System.out.println(str1+str2+"ay");

}

Question 2: Write a program in Java to enter a String.  Prnt the String in alphabetical order of its letters.

Sample Input: COMPUTER

Sample Output: CEMOPRTU

Solution:-  

/// To arrange the letters of  String in alphabetical order

public class Letters

{

      public static void main(String args[])

      {

        int i,j,p;

        char ch;

        p=str.length();

        System.out.println("The Letters arranged in alphabetical order....");

        for(i=65;i<=90;i++)     

        {

            for(j=0;j<p;j++)     

            {

                  ch=str.charAt(j);

                    if(ch==(char)i||(char)(i+32)

                    System.out.println(ch);

            }

       }

}          


Question 3: Write a program in Java to accept a sentence in lower case. Convert first letter of each word of the sentence in upper case. Display the new senetence so formed.

Sample Input: we are in cyber world

Sample Output: We Are In Cyber World                                                             [ICSE 2018]

Solution:-

public class ConvertSentenceCase

{

      public static void main(String args[])

      {

String str1="";

        int i,p;

        char chr,chr1;

        str=' '+str;

        p=str.length();

        for(i=0;i<p;i++)

       {

           chr=str.charAt(i);

           if(chr==' ')

            {

                char1=str.charAt(i+1);

                str=str1+'  '+Character.tiUpperCase(chr1);

                i=j+1;

            else

               str1=str1+chr;

          }

                    System.out.println(""The new String after changing the first letter of the word:");

                    System.out.println(str1);

}

}

Question 4: Write a program to input a sentence and it into uppercase and count and display the total number of words starting with a letter 'A'.

                                                                                                                        [ICSE 2019]

Example:

Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING.

Sample Output: Total number of words starting with letter 'A' = 4.

Ans.

import java.util."; 

import java.util.Scanner;

public class WordsInSentence

{

     public static void main(String args[])

     {

        Scanner scan = new Scanner(System.in);

char ch int i; 

        System.out.println("Type a sentence: "); 

String sentence = scan.nextLine(); 

System.out.println("You typed: "+ sentence.toUpperCase());

        String capsen=sentence.toUpperCase():

int count=0;

char ch[]= new char[capsen.length()];

for(String st: capsen.split(" "))

{

if(st.startsWith("A"))

{

   count++;

   System.out.println(st);

}

}

System.out.println("Total Number of words starting with '\A'\ is : "+coount);

        }

     }


Question 5: Write a program to input a string and print out the text with te uppercase andlower case letters reversed, but all other characters should be remain the same as before.

Example:

Input: The girl IS DancInG

Output:tHE GIRL is dANCiNg

Solution:-

import java.util."; 

public class ULToggle

{

     public static void main(String args[])

     {

        //user input

        Scanner scan = new Scanner(System.in);

System.out.println("Enter a sentence : ");

        String sentence=sc.nextLine();

        //toggle the case

        String result="";

        char ch;

        int i;

        for(i=0;i<sentence.length();i++)

        {

           ch=sentence.charAt(i);

           if(Character.isUpperCase(ch))

            ch=Character.toLowerCase(ch);

           else 

              ch=Character.toUpperCase(ch));

       result=result+ch;

System.out.println("Result :"+result);

      }

Question 6: Write a program to input a sentence and print the number of character found in the longest word of the given sentence. For example, if S-"Health is the most important thing in life", the nthe output sholud be 9.
Ans:-

import java.util."; 

public class FindLongestWord

{

     public static void main(String args[])

     {

        //user input

        char ch;

        str=str+" ";

        int indx=0;

        int x=str.length();

        System.out.println("Enter a sentence : "+str);

        System.out.println("\n");

        String f=" ";

        for(int i=0;i<x;i++)

{

           ch=str.charAt(i);

           if(ch==' ')

           {

                String z=str.substring(indx,i);
                if(z.length()>len;
                {
                        len=z.length();
                         f=z;
                }
                 indx=i+1;
        }
    }
        System.out.println(f+"  "+len);
   }
}

Question 7: Write a program in Java to read a string from console and replace  all occurences of the fifth character of the string by the digit 5.
Ans:-

import java.io.*;

import java.util."; 

public class CharacterDigit 

{

     void replaceString() throws IOException

     {

        Scanner scan = new Scanner(System.in);

System.out.println("String is  : ");

        String sentence=sc.nextLine();

        char x=n.charAt(4);

        String chr=n.replace(x, 5);

        System.out.println("The output string is :"+chr);

        }

        public static void main(String args[]) throws IOException

        {

            CharacterDigit c=new CharacterDigit();

            c.replaceString();

        }        

}


Question 8: Define a class to accept a string and convert it into uppercase. Count and display the number of vowels in it.                                                                [ICSE 2024]
Input: robotics
Output: ROBOTICS
Number of vowels: 3 
Ans:-
import java.util.Scanner;
public class CountVowels
{
    public static void main(String args[]) 
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the string: ");
        String str = in.nextLine();
        str = str.toUpperCase();
        str += " ";
        int count = 0;
        int len = str.length();
        
        for (int i = 0; i < len - 1; i++) 
        {
            char ch = str.charAt(i);
            if(ch == 'A' 
                || ch == 'E' 
                || ch == 'I'
                || ch == 'O'
                || ch == 'U')
                count++;
        }
        
        System.out.println("String : " + str);
        System.out.println("Number of vowels : " + count);
    }
}

Question 9: Define a class to declare a character array of size ten. Accept the characters into the array and display the characters with highest and lowest ASCII (American Standard Code for Information Interchange) value.
EXAMPLE :
INPUT:
'R', 'z', 'q', 'A', 'N', 'p', 'm', 'U', 'Q', 'F'
OUTPUT :
Character with highest ASCII value = z
Character with lowest ASCII value = A
Ans:-
import java.util.Scanner;

public class ASCIIEx
{

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        char ch[] = new char[10];
        int len = ch.length;
        
        System.out.println("Enter 10 characters:");
        for (int i = 0; i < len; i++)
        {
            ch[i] = in.nextLine().charAt(0);
        }
        
        char h = ch[0];
        char l = ch[0];
        
        for (int i = 1; i < len; i++)
        {
            if (ch[i] > h)
            {
                h = ch[i];
            }
            
            if (ch[i] < l)
            {
                l = ch[i];
            }
        }

        System.out.println("Character with highest ASCII value: " + h);
        System.out.println("Character with lowest ASCII value: " + l);
    }

}
Question 10: Define 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
Ans:-
import java.util.Scanner;

public class CountAll
{
   public static void main(String args[]) 
   {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a string:");
        String str = in.nextLine();
             
        int len = str.length();

        int ac = 0;
        int sc = 0;
        int dc = 0;
        char ch;

        for (int i = 0; i < len; i++) {
            ch = str.charAt(i);
            if (Character.isLetter(ch))  
                ac++;    
            else if (Character.isDigit(ch))
                dc++;
            else if (!Character.isWhitespace(ch))
                sc++;
        }
        
        System.out.println("No. of Digits = " + dc);
        System.out.println("No. of Alphabets = " + ac);
        System.out.println("No. of Special Characters = " + sc);
        
    }
}

No comments:

Post a Comment

CLASS X TYPES OF NUMBERS

 CLASS X  TYPES OF NUMBERS List of types of Numbers 1. Arm Strong Number 2.  Co-prime Number 3. Duck Number [I.C.S.E 2024] 4. Magic Number  ...