Tuesday, 18 July 2023

Java String Program - Part 1

 Program 1


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

import java.util.Scanner;

class P1{
    
    public static void change(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a sentence");
        
        String s = sc.nextLine();
        String newstr = "";
        
        s = s.toLowerCase();
        s = s.trim();
        s = " " + s;
        
        for(int i=0; i<s.length(); i++){
            char ch = s.charAt(i);
            if(ch == ' '){
                newstr = newstr + ch;
                newstr = newstr + Character.toUpperCase(s.charAt(i+1));
                i++;
            }
            else {
                newstr = newstr + ch;
            }
        }
        
        System.out.println("new string = " + newstr);
    }
}

Program 2

Write a program to accept a string. Convert the string into upper case letters. Count and output the number of double letter sequences that exist in the string
sample Input: SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
Sample Output:4

import java.util.Scanner;

class P2{
    
    public static void countDouble(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a sentence");
        String s = sc.nextLine();
        
        int count = 0;
        
        s = s.toUpperCase();
        
        for(int i =0; i<s.length()-1 ; i++){
            char ch = s.charAt(i);
            char ch1 = s.charAt(i+1);
            if(ch == ch1){
                count++;
            }
        }
        
        System.out.println("count of double letter seq = " + count);
    }}
        

Program 3

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 same from left to right and vice-versa.
Example: MALYALAM, 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 display whether the word is a palindrome or only a special word or none of them.

import java.util.Scanner;

class P3{
    
    public static void check(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a word");
        String w = sc.next();
        
        boolean isSpecial = false;
        boolean isPalin = false;
        
        // decide if the word is a special word
        
        char fc = w.charAt(0);
        char lc = w.charAt(w.length()-1);
        
        if (fc == lc)
            isSpecial = true;
            
        String rev = "";
        for(int i = w.length()-1; i >= 0; i--){
            rev += w.charAt(i);
        }
        
        if(w.equals(rev))
            isPalin = true;
        
        if( isSpecial && isPalin )
            System.out.println(w + " is special and palindrome ");
        else if(isSpecial)
            System.out.println(w + " is special");
        else if(isPalin)
            System.out.println(w + " is palindrome ");
        else
            System.out.println(w + " is neither special nor palindrome ");
        }}   


Video explanations for these programs 



 

            

            
            
        


No comments:

Post a Comment

Infinix GT 20 Pro - Budget Gaming Phone?

  Gamers, strap yourselves in! Calling all mobile warriors and esports enthusiasts, the Infinix GT 20 Pro has just entered the arena. This r...