In this program we will calculate the number of spaces or white spaces in the string .
For example , if the string is
"Alive is awesome"
then the answer is 2 .
This is quite useful when some one takes the input from the user such as first name , last name or any other input in string format .
Here we are passing String to the spacecount() function which return the int value that is the number of white spaces in the string .
Please find the code below :
For example , if the string is
"Alive is awesome"
then the answer is 2 .
This is quite useful when some one takes the input from the user such as first name , last name or any other input in string format .
Here we are passing String to the spacecount() function which return the int value that is the number of white spaces in the string .
Please find the code below :
public class SpaceCount { static int i,c=0,res; static int spacecount(String s) { for(i=0,c=0;i<s.length();i++) { char ch=s.charAt(i); if(ch==' ') c++; } return c; } public static void main (String args[]) { res=SpaceCount.spacecount("i am adult "); //string is always passed in double quotes System.out.println("The number of white spaces in the String are : "+res); } }