Drop Down

Monday, February 4, 2019

Count_Character_WithoutLoop

package interview;
//Write a program to find the number of occurrences of a given character in a string without using loop
public class Count_Character_WithoutLoop {

public static void main(String[] args)
{
  String str = "This is an apple";
      System.out.println("Length Of String:" + str.length());
      System.out.println("Length Of String Without a :" + str.replace("a", "").length());
      //System.out.println(">>>"+str.replace("a", ""));
      int charcount = str.length() - str.replaceAll("a", "").length();
      System.out.println("Occurrence Of A Char In String: " + charcount);

}

}




     

No comments:

Post a Comment

Java 8 Notes Pics