Wednesday, January 16, 2008

Count the number of spaces in a String

This Program count the number of spaces in a String

import java.io.*;
class spaces
{
public static void main(String args[])throws IOException
{
int c=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter a String ");
String s = br.readLine();
int l= s.length();
for(int i=0;i{
char ch=s.charAt(i);
if(ch==' ')
c=c+1;
}
System.out.println("Number of spaces " + c);
}
}

No comments: