Coders Packet

Information about given string in Java

By Sanjana Sakinala

This source code is written in Java and is about strings. We get to know various information about the given string.

Source code to find information about given data

The given code tells you information about the given string. It is written in Java and accepts the string using the scanner class. We have imported util package in order to use the scanner class. At first, the input is given by the user, the program is compiled and then we get information about the string like number of vowels, characters, spaces, words, and consonants mentioned in the string. With just a click we can know about the whole information. Please use an offline compiler in order to run this program as it may be difficult to work with importing packing in online compilers.

import java.util.*;
public class CountAboutStrings
{
public static void main(String[] args)
{
int count=0, space=0, vowelcount = 0, consonantcount = 0;
Scanner sc= new Scanner(System.in);
System.out.print("Enter a string: ");
String str= sc.nextLine();
str = str.toLowerCase();

for(int i = 0; i < str.length(); i++)
{
{
if(str.charAt(i) != ' ')
{
count++;
}
else
{
space ++ ;
}
}
if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u')
{
vowelcount++;
}
else if(str.charAt(i) >= 'a' && str.charAt(i)<='z')
{
consonantcount++;
}

}
System.out.println("Total number of characters/letters in a string: " + count);
System.out.println("Number of space: " + space);
System.out.println("Number of words: " +(space+1));
System.out.println("Number of vowels: " + vowelcount);
System.out.println("Number of consonants: " + consonantcount);
}
}

Download project

Reviews Report

Submitted by Sanjana Sakinala (sanjana28)

Download packets of source code on Coders Packet