Wednesday, 11 March 2015

[JAVA] [ICSE 2005 Answer 5] Calculating Number Of Vowels & Reverse String

Question:
ICSE 2005 Question 5
Write a program to input any given string to calculate the total number of characters and vowels present in the string and also reverse the string.
Example:
INPUT
Enter string : SNOWY
OUTPUT
Total number of characters :  05
Name of vowels             :  01
Reverse string             :  YWONS

Source Code:
import java.util.*;
public class ICSE2005_5
{
    public static void main(String args[])
    {
        Scanner read=new Scanner(System.in);
        System.out.print("Enter string : ");
        String n=read.next();
        int length=n.length();
        String reverse="";
        int vowel=0;
        for(int i=(length-1); i>=0; i--)
        {
            char c=n.charAt(i);
            if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
                vowel++;
            reverse+=c;
        }
        System.out.println("Total number of characters : \t"+length+"\nName of vowels : \t\t"+vowel+"\nReverse string : \t\t"+reverse);
    }
}


Result:
OUTPUT:
Enter string : Pneumonoultramicroscopicsilicovolcanoconiosis
Total number of characters : 45
Name of vowels :                    20
Reverse string :                       sisoinoconaclovociliscipocsorcimartluonomuenP

1 comment: