Hackerss.com

hackerss
hackerss

Posted on

Java Contar cuantos caracteres tiene un string


import java.io.*;

public class CountCharacters {
   /**
   * This method count the characters of a string.
   * @param stringToCount the string to count.
   * @return int Obtains the number of characters of the string.
   */   
   public int countCharacters(String stringToCount) {
       //Variable to store the total sum
       int total = 0;
       //Sum the two numbers
       total = stringToCount.length();
       //Return the total
       return total;
   }

   /**
   * One Example
   */
   public static void main(String args[]) throws IOException {

      CountCharacters  countCharacters= new CountCharacters ();
      //Example
      int sum = countCharacters.countCharacters("Hola");
      //Output
      System.out.println("Sum of 10 and 20 is :" + sum); //30
   }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)