Monday, July 10, 2023

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 which read the same from left to right and vice versa
Examples:
MALAYALAM
MADAM
LEVEL
ROTATOR
CIVIC
All palindromes are special words, but all special words are not palindromes. Write a program to accept a word check and print Whether the word is a palindrome or only special word.

Solution:
import java.util.Scanner;
public class Year2016Q1
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String str = sc.next();
        str = str.toUpperCase();
        int len = str.length();
        if (str.charAt(0) == str.charAt(len - 1))
        {
            boolean isPalin = true;
            for (int i=1;i<len/2;i++)
            {
                if (str.charAt(i)!=str.charAt(len-1-i))
                {
                    isPalin = false;
                    break;
                }
            }
            if (isPalin)
            {
                System.out.println("Palindrome");
            }
            else
            {
                System.out.println("Special");
            }
        }
        else
        {
            System.out.println("Neither Special nor Palindrome");
        }
    }
}

Output:








String program on Function Overloading

Design a class to overload a function Joystring( ) as follows :

(i) void Joystring (String s, char ch1 char ch2) with one string argument and two character arguments that replaces the character argument ch1 with the character argument ch2 in the given string s and prints the new string.

Example:
Input value of s = “TECHNALAGY”
ch1=‘A’,
ch2=‘O’
Output: TECHNOLOGY

(ii) void Joystring (String s) with one string argument that prints the position of the first space and the last space of the given string s.

Example:
Input value of = “Cloud computing means Internet based computing”
Output: First index : 5
Last index : 36

(iii) void Joystring (String s1, String s2) with two string arguments that combines the two string with a space between them and prints the resultant string. 

Example :
Input value of s1 =“COMMON WEALTH”
Input value of s2 =“GAMES”
Output: COMMON WEALTH GAMES
(use library functions) 

Solution:
We have tried to do this code in 2 methods:
Method 1: Using library functions
Method 2: Without using library functions

Note: Use anyone of the methods.

public class Year2015Q1
{

//  Method 1: Using library functions
    void Joystring(String s,char ch1,char ch2)
    {
        String newStr="";
        int len=s.length();
        newStr=s.replace(ch1, ch2);
        System.out.println(newStr);
    }
   
    void Joystring(String s)
    {
        int firstSpaceIndex=0,lastSpaceIndex=0;
        firstSpaceIndex=s.indexOf(' ');
        lastSpaceIndex=s.lastIndexOf(' ');
        System.out.println("First Index: "+firstSpaceIndex);
        System.out.println("Last Index: "+lastSpaceIndex);
    }
   
    void Joystring(String s1,String s2)
    {
        System.out.println(s1.concat(" ").concat(s2));
    }
   
//  Method 2: Without using library functions
    void Joystring(String s,char ch1,char ch2)
    {
        String newStr="";
        int len=s.length();
        for(int i=0;i<len;i++)
        {
            char ch=s.charAt(i);
            if(ch==ch1)
            {
                newStr=newStr+ch2;
            }
            else
            {
                newStr=newStr+ch;
            }
        }
        System.out.println(newStr);
    }
   
    void Joystring(String s)
    {
        int len=s.length();
        int firstSpaceIndex=0,lastSpaceIndex=0;
        for(int i=0;i<len;i++)
        {
            char ch=s.charAt(i);
            if(ch==' ')
            {
                firstSpaceIndex=i;
                break;
            }
        }
        for(int i=len-1;i>=0;i--)
        {
            char ch=s.charAt(i);
            if(ch==' ')
            {
                lastSpaceIndex=i;
                break;
            }
        }
        System.out.println("First Index: "+firstSpaceIndex);
        System.out.println("Last Index: "+lastSpaceIndex);
    }
   
    void Joystring(String s1,String s2)
    {
        System.out.println(s1+" "+s2);
    }
   
    public static void main(String[] args)
    {
        Year2015Q1 ob = new Year2015Q1();
        ob.Joystring("TECHNALAGY",'A','O');
        ob.Joystring("Cloud computing means Internet based computing");
        ob.Joystring("COMMON WEALTH","GAMES");
    }
}

Output:



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:


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