This is a program to count number of Words in a Entered String.
Source Code:
import java.io.*;
class Count
{
public static void main(String args[])
{
String s;
int count=0;
System.out.print("Enter a String: ");
Console con=System.console();
s=con.readLine();
char arr[]=s.toCharArray();
for(int i=0;i<arr.length;i++)
{
if(arr[i]==' ')
count++;
}
System.out.print("There are "+(count+1)+" words in Entered String");
}
}
Logic:We know that there is one blank space in between two words. Using the same fact we have created the program in which we count number of spaces and increase it by 1 in final result.
No comments:
Post a Comment