Exercise
Function returning a value V2
Objetive
Write a java program whose Main must be like this:
public static void Main()
{
__Console.WriteLine("\"Hello, how are you\" contains {0} spaces", ____CountSpaces("Hello, how are you") );
}
CountSpaces is a function that you must define and that will be called from inside Main.
As you can see in the example, it must accept an string as a parameter, and it must return an integer number (the amount of spaces in that string).
Example Code
public class Main
{
public static int CountSpaces(String text)
{
int countSpaces = 0;
String letter;
for (int i = 0; i < text.length(); i++)
{
letter = text.substring(i, i + 1);
if (letter.equals(" "))
{
countSpaces++;
}
}
return countSpaces;
}
public static void main(String[] args)
{
System.out.printf("\"Hello, how are you\" contains %1$s spaces" + "\r\n", CountSpaces("Hello, how are you"));
}
}