https://www.compilejava.
net/
Write a Java program to count the number of words in a string?
public class WordCount {
static int wordcount(String string)
{
int count=0;
char ch[]= new char[string.length()];
for(int i=0;i<string.length();i++)
{
ch[i]= string.charAt(i);
if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )
count++;
}
return count;
}
public static void main(String[] args) {
String string =" Hi How are you I am fine";
System.out.println(wordcount(string) + " words.");
}
}
Java Program to count the total number of characters in a string
public class CountCharacter
{
public static void main(String[] args) {
String string = "The best of both worlds";
int count = 0;
//Counts each character except space
for(int i = 0; i < string.length(); i++) {
if(string.charAt(i) != ' ')
count++;
}
//Displays the total number of characters present in the given string
System.out.println("Total number of characters in a string: " + count);
}
}
Print characters and their frequencies in order of occurrence
Input : str = "elephant"
Output : e2 l1 p1 h1 a1 n1 t1
// Java implementation to print the character and
// its frequency in order of its occurrence
public class Char_frequency {
static final int SIZE = 26;
// function to print the character and its
// frequency in order of its occurrence
static void printCharWithFreq(String str)
{
// size of the string 'str'
int n = str.length();
// 'freq[]' implemented as hash table
int[] freq = new int[SIZE];
// accumulate freqeuncy of each character
// in 'str'
for (int i = 0; i < n; i++)
freq[str.charAt(i) - 'a']++;
// traverse 'str' from left to right
for (int i = 0; i < n; i++) {
// if frequency of character str.charAt(i)
// is not equal to 0
if (freq[str.charAt(i) - 'a'] != 0) {
// print the character along with its
// frequency
System.out.print(str.charAt(i));
System.out.print(freq[str.charAt(i) - 'a'] + " ");
// update frequency of str.charAt(i) to
// 0 so that the same character is not
// printed again
freq[str.charAt(i) - 'a'] = 0;
}
}
}
// Driver program to test above
public static void main(String args[])
{
String str = "geeksforgeeks";
printCharWithFreq(str);
}
}
public class Frequency {
public static void main(String[] args) {
String str = "This website is awesome.";
char ch = 'e';
int frequency = 0;
for(int i = 0; i < str.length(); i++) {
if(ch == str.charAt(i)) {
++frequency;
}
}
System.out.println("Frequency of " + ch + " = " + frequency);
}
}
Program to Remove All Whitespaces
public class Whitespaces {
public static void main(String[] args) {
String sentence = "T his is b ett er.";
System.out.println("Original sentence: " + sentence);
sentence = sentence.replaceAll("\\s", "");
System.out.println("After replacement: " + sentence);
}
}
We've used regular expression \\s that finds all white space characters (tabs, spaces, new line
character, etc.) in the string. Then, we replace it with "" (empty string literal).
Program to count vowels, consonants, digits and spaces
public class Count {
public static void main(String[] args) {
String line = "This website is aw3som3.";
int vowels = 0, consonants = 0, digits = 0, spaces = 0;
line = line.toLowerCase();
for(int i = 0; i < line.length(); ++i)
{
char ch = line.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u') {
++vowels;
}
else if((ch >= 'a'&& ch <= 'z')) {
++consonants;
}
else if( ch >= '0' && ch <= '9')
{
++digits;
}
else if (ch ==' ')
{
++spaces;
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
System.out.println("Digits: " + digits);
System.out.println("White spaces: " + spaces);
}
}