Sunday, July 9, 2023

Write a program in Java to accept a string in lower case and change the first letter of every word to upper case. Display the new string.

Write a program in Java to accept a string in lower case and change the first letter of every word to upper case. Display the new string. 

Sample input:        we are in cyber world
Sample output :     We Are In Cyber World

Video Link:




Solution:
import java.util.Scanner;
public class Year2018Q1
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a sentence:");
        String str = sc.nextLine();
        str=" "+str;
        String word = "";
        for (int i = 1; i < str.length(); i++)
        {
            if (str.charAt(i - 1) == ' ')
            {
                word = word + Character.toUpperCase(str.charAt(i));
            }
            else
            {
                word = word + str.charAt(i);
            }
        }
        System.out.println(word);
    }
}

Output:

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

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'.
Example:
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING.
Sample Output : Total words starting with letter 'A' = 4.

Solution:
import java.util.Scanner;
public class Year2019Q1
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a string: ");
        String str = in.nextLine();
        str = " " + str;
        int count = 0;
        int len = str.length();
        str = str.toUpperCase();
        for (int i = 0; i < len - 1; i++)
        {
            if (str.charAt(i) == ' ' && str.charAt(i + 1) == 'A')
            {
                count++;
            }
        }
        System.out.println("Total words starting with letter 'A' = " + count);
    }
}

Output:




Define a class to accept two strings of same length and form a new word in such a way that, the first character of the first word is followed by the first character of the second word and so on.

Define a class to accept two strings of same length and form a new word in such a way that, the first character of the first word is followed by the first character of the second word and so on.
Example:
Input string 1-BALL
Input String 2- WORD
OUTPUT: BWAOLRLD

Solution:
import java.util.Scanner;

public class Year2022Q3
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String st1,st2;
        String newStr = "";
        System.out.println("Enter 2 string of same length: ");
        System.out.print("Enter first string:");
        st1 = sc.nextLine();
        System.out.print("Enter second string:");
        st2 = sc.nextLine();
        int len = st1.length();
        for (int i = 0; i < len; i++)
        {
            newStr = newStr + st1.charAt(i) + st2.charAt(i);
        }
        System.out.println(newStr);
    }
}

Output:

Define a class to declare an array to accept and store ten words. Display only those words which begin with the letter 'A' or 'a' and also end with the letter 'A' or 'a'.

Define a class to declare an array to accept and store ten words. Display only those words which begin with the letter 'A' or 'a' and also end with the letter 'A' or 'a'.

EXAMPLE:
Input: Hari, Anita, Akash, Amrita, Alina, Devi, Rishab, Jolin, Farha, AMITHA
Output:
Anita
Amrita
Alina
AMITHA

Solution:
import java.util.Scanner;

public class Year2022Q2
{
    public static void main(String[] args)
    {
        String names[] = new String[10];
        Scanner sc = new Scanner(System.in);
        int len = names.length;
        System.out.println("Enter 10 name:");
        for (int i = 0; i < len; i++)
        {
             names[i]=sc.nextLine();
        }
        System.out.println("Names are:");
        for (int i = 0; i < len; i++)
        {
            int lastIndex = names[i].length() - 1;
            if((names[i].charAt(0)=='a'
                ||names[i].charAt(0)=='A')
                &&(names[i].charAt(lastIndex)=='a'
                ||names[i].charAt(lastIndex)=='A'))
            {
                System.out.println(names[i]);
            }
        }
    }
}

Output:



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

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.


EXAMPLE:
INPUT: WelCoMe_2022
OUTPUT: wELcOmE_2022

Video Link:


Solution:
import java.util.Scanner;
public class Year2022Q1
{
    public static void main(String[] args)
    {
       
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string: ");
        String str = sc.nextLine();
        String newStr="";
        int len=str.length();

//      Method 1
        for(int i=0;i<len;i++)
        {
            char ch=str.charAt(i);
            if(Character.isUpperCase(ch))
                newStr = newStr + Character.toLowerCase(ch);
            else if(Character.isLowerCase(ch))
                newStr += Character.toUpperCase(ch);
            else
                newStr+=ch;
        }
       
//      Method 2
        for(int i=0;i<len;i++)
        {
            char ch=str.charAt(i);
            if(ch>='A' && ch<='Z')
                newStr = newStr + Character.toLowerCase(ch);
            else if(ch>='a' && ch<='z')
                newStr += Character.toUpperCase(ch);
            else
                newStr+=ch;
        }
        System.out.println(newStr);
    }
}

Output:


Saturday, July 8, 2023

Define a class to accept a string and print number of digits, alphabets and special characters in the string.

Define a class to accept a string and print number of digits, alphabets and special characters in the string.
Example: 
Input:     S = KAPILDEV@83
Output:  Number of Digits - 2
              Number of Alphabets - 8
              Number of Special Characters - 1




                 
Solution:

import java.util.Scanner;
public class Year2023Q1
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string:");
        String str = sc.nextLine();
        int len=str.length();
        int digitCount=0,letterCount=0,whitespaceCount=0,spclCharCount=0;
       
//      Method 1
        for(int i=0;i<len;i++)
        {
            char ch=str.charAt(i);
            if(ch>='0' && ch<='9')
                digitCount++;
            if(ch>='A' && ch<='Z'|| ch>='a' && ch<='z')
                letterCount++;
            if(ch==' ')
                whitespaceCount++;
            spclCharCount=len-(digitCount+letterCount+whitespaceCount);
        }
       
//      Method 2    
        for(int i=0;i<len;i++)
        {
            char ch=str.charAt(i);
            if(Character.isDigit(ch))
                digitCount=digitCount+1;
            if(Character.isLetter(ch))
                letterCount+=1;
            if(Character.isWhitespace(ch))
                whitespaceCount++;
            spclCharCount=len-(digitCount+letterCount+whitespaceCount);
        }
       
        System.out.println("Number of digits = "+digitCount);
        System.out.println("Number of letters = "+letterCount);
        System.out.println("Number of special Char = "+spclCharCount);
        System.out.println("Number of whitespace = "+whitespaceCount);  
    }
}

Output:


Saturday, November 27, 2021

Java MCQs Part-01 | By LearnWithVishalGiri | ICSE Computer Application




1. Name the method that accepts a string without any space _________
2. The wrapper class to which the method parseInt() belongs to _____________
    a)integer
    b)Int 
    c)int
    d)Integer
3. The statement to stop the execution of a construct.
    a)System.exit(0)
    b)break
    c)continue
    d)Stop
4. !(2>1 && 5<7)
    a)true
    b)false
5. The default statement is optional in switch case.
    a)True
    b)False
6. The shorthand operator is left to right associative.
    a)True
    b)False
7. Tell the output:

8. Give the output: (X = 5)
X+ = X++ + ++X + --X +X
    a)29
    b)28
    c)26
    d)25
9. Give the output and convert into if else statement.




Check whether the word is Special word or Palindrome word.

Special words are those words which starts and ends with the same letter.  Examples: EXISTENCE COMIC WINDOW Palindrome words are those words...