Words And Spaces In A String
Write a program to enter a string and display the number of words and blank spaces in it.
/* WAP to enter a string and display the number of words and blank spaces in it. */
import java.io.*;
class StringQ01
{
public static void main(String arg[])throws IOException
{
int i, l, bs;
String str;
char ch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string : ");
str=br.readLine();
l = str.length();
// To count no of blank spaces
bs = 0;
for(i=0;i < l;i++){
ch = str.charAt(i);
//Count blank spaces
if(ch == ' ')
bs++;
}
System.out.println("No of blank spaces: "+bs);
System.out.println("No of words: "+(bs+1));
}//end of main
}//end of class
Have something to say? Log in to comment on this post.
0 comments