String | VI.31a — August 29, 2017

String | VI.31a

(a)
BLUEJ
BLUE
BLU
BL
​B

​Code :


import java.util.*;
class V31a {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a Word.");
        String str = in.nextLine();
        for(int i=str.length()-1;i>=0;i--) {
            for(int j=0;j<=i;j++) {
                System.out.print(str.charAt(j));
            }
            System.out.print("\n");
        }
    }
}
String | VI.30 —

String | VI.30

30. Write a program to input a string. Convert the string to upper case. Count and print the words which have at least a pair of consecutive letters.

Sample Input : MODEM IS AN ELECTRONIC DEVICE
Sample Output :
MODEM
DEVICE
Number of words containing consecutive letters : 2

Code :


import java.util.*;
class V30 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter String :");
        String str = in.nextLine().toUpperCase();
        String word[] = str.split(" ");
        int count =0;
        System.out.println("The Words with consecutive letters :"); 
        for(int i=0;i<word.length;i++) {
            for(int j=0;j<word[i].length()-1;j++) {
                char a = word[i].charAt(j);
                char b = word[i].charAt(j+1);
                if(b-a==1){
                    count++;
                    System.out.println(word[i]);
                }
            }
        }
        if(count==0) {
            System.out.println("No Word with consecutive letters.");
        }
        else {
            System.out.println("No. of words with consecutive letters:"+count);
        }
    }
}
String | VI.29 —

String | VI.29

29. 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
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. Print and display whether the word is a palindrome or only a special word.

Code :


import java.util.*;
class V29 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter String:");
        String str = in.nextLine().toUpperCase();
        if(str.charAt(0)==str.charAt(str.length()-1)) {
            String rev="";
            for(int i=str.length()-1;i>=0;i--) {
                rev+=str.charAt(i);
            }
            if(str.equals(rev)){
                System.out.println("Palindrome String.");
            }
            else {
                System.out.println("Special String.");
            }
        }
        else {
            System.out.println("Not a Special String.");
        }
    }
}
String | VI.28 —

String | VI.28

28. Write a program to accept a string. Convert the string to upper case. 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

Code :


import java.util.*;
class V28 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter String :");
        String str = in.nextLine();
        int count=0;
        for(int i=0;i<str.length()-1;i++) {
            if(str.charAt(i)==str.charAt(i+1)) {
                count++;
            }
        }
        System.out.println("No. of Double Sequence :"+count);
    }
}
String | VI.27 —

String | VI.27

27. Write a program to input a string. Count and display the frequency of each letter in the order in which it is present in the String.

Sample Input : DADDY’S CODE
Sample Output :
D : 3
A : 1
Y : 1
S : 1
C : 1
O : 1
D : 1
​E : 1

Code :


import java.util.*;
class V27 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter String :");
        String str = in.nextLine().toUpperCase();
        for(char i='A';i<='Z';i++) {
            int count=0;
            for(int j=0;j<str.length();j++) {
                char c = str.charAt(j);
                if(c==i) {
                    count++;
                }
            }
            if(count!=0) {
                System.out.println(i+" :"+count);
            }
        }
    }
}
String | VI.26 —

String | VI.26

26. A non-palindrome word can be made a palindrome word just by adding the reverse of the word to the original word. Write a program to accept a non-palindrome word and display the new word after making it a palindrome.

Sample Input :
ICSE
Sample Output :
The new word making it palindrome as:
ICSEESCI

Code :


import java.util.*;
class V26 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter String :");
        String str = in.nextLine().toUpperCase();
        String rev ="";
        for(int i=str.length()-1;i>=0;i--) {
            rev+=str.charAt(i);
        }
        if(str.equals(rev)) {
            System.out.println("It is Palindrome String.");
        }
        else {
            System.out.println("It is not a Palindrome String.");
            System.out.println("Palindrome String :"+(str+rev));
        }
    }
}
String | VI.25 —

String | VI.25

25. A “Happy Word’ is defined as:
Take a word and calculate the word’s value based on position of the letters in English alphabet. On the basis of word’s value, find the sum of the squares of its digits. Repeat the process with the resultant number until the number equals 1 (one). If the number ends with 1 then it is called a ‘Happy Word’. Write a program to input a word and check whether it a ‘Happy Word’ or not. The program displays a message accordingly.

Sample Input:
VAT
Place value of V=22, A=1, T=20
[Hint: A=1, B=2,……….Z=26]

Solution: 22120 = 2^2+2^2+1^2+2^2+0^2 = 13 = 1^2 + 3^2 = 10 = 1^2+0^2 = 1
Sample Output : A Happy Word

Code :


import java.util.*;
class V25 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter String :");
        String str = in.nextLine().toUpperCase();
        String sum = "";
        for(int i=0;i9) {
            int sumd=0;
            while(sumn!=0) {
                int d = sumn%10;
                sumd+=d*d;
                sumn/=10;
            }
            sumn=sumd;
        }
        if(sumn==1) {
            System.out.println("Happy String.");
        }
        else {
            System.out.println("Not a Happy String.");
        }
    }
}    
String | VI.24 —

String | VI.24

24. Write a program to input a sentence in uppercase. Create a new sentence by replacing each vowel with the next (ie. A with E, U with A) and the other characters remain the same. Display the new sentence accordingly.

Sample Input : WE CELEBRATE 26 JANUARY AS REPUBLIC DAY
Sample Output : WI CILIBRETI 26 JENAERY ES RIPABLOC DEY

Code :


import java.util.*;
class V24 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter String :");
        String str = in.nextLine().toUpperCase();
        String nstr ="";
        for(int i=0;i<str.length();i++) {
            char c = str.charAt(i);
            switch (c) {
                case 'A':
                c='E';
                break;
                case 'E':
                c='I';
                break;
                case 'I':
                c='O';
                break;
                case 'O':
                c='U';
                break;
                case 'U':
                c='A';
                break;
            }
            nstr+=c;
        }
        System.out.println(nstr);
    }
}        
String | VI.23 —

String | VI.23

23. A string is said to be ‘Unique’ if none of the letters present in the string are repeated. Write a program to accept a string and check whether the string is Unique or not. The program displays a message accordingly.

Sample Input : COMPUTER
Sample Output : Unique String

Code :


import java.util.*;
class V23 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter String:");
        String str = in.nextLine();
        boolean flag=true;
        for(int i=0;i<str.length();i++) {
            char c = str.charAt(i);
            for(int j=i+1;j<str.length();j++) {
                char d = str.charAt(j);
                if(c==d) {
                    flag=false;
                    break;
                }
            }
        }
        if(flag) {
            System.out.println("Unique String.");
        }
        else {
            System.out.println("Not an Unique String.");
        }
    }
}
String | VI.22 —

String | VI.22

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

Sample Input : computer
Sample Output : cpmpvtfr

Code :


import java.util.*;
class V22 {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a String :");
        String str = in.nextLine().toLowerCase();
        String nstr="";
        for(int i=0;i<str.length();i++) {
            char c = str.charAt(i);
            switch(c) {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                c++;
            }
            nstr+=c;
        }
        System.out.println(nstr);
    }
}