Java code snippet to count number of spaces in a string

import java.util.*;
class CountSpaces
{
public static void main(String s[])
{
Scanner sc = new Scanner(System.in);
int count = 0;
System.out.println("Enter The String To Count No. Of Spaces : ");
String str = sc.nextLine();
for(int i=0;i<str.length();i++)
{
if(str.charAt(i) ==' ') count++;
}
System.out.println("The no of Spaces in given string is : " + count);
}
}

Comments