120 Program to count number of white space character in character stream in Java.

Code:
/*
Program to count number of whitespace
character in character stream.
*/

import java.io.*;

class CountDemo
{
public static void main(String args[]) throws Exception
{
char arr[] = "C o d i n g Atharva".toCharArray();

CharArrayReader rd = new CharArrayReader(arr);
CharArrayWriter rw = new CharArrayWriter();

int i , count = 0;

i = rd.read();

while(i != -1)
{
char ch = (char)i;
rw.write(i);

if(ch==' ')
count++;

i = rd.read();
}

System.out.println("\n"+rw);
System.out.println("\nNumber of whitespace:"+count);

}
}

Ouput:

count number of white space character in character stream in Java.

Previous
Next Post »