occurrences – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 06:39:26 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg occurrences – Programmerbay https://programmerbay.com 32 32 program to count a particular word occurrence in a Sequence of string without using loop https://programmerbay.com/program-to-count-a-particular-word-occurrence-in-a-string-without-using-loop/ https://programmerbay.com/program-to-count-a-particular-word-occurrence-in-a-string-without-using-loop/#respond Thu, 27 Feb 2020 07:19:02 +0000 https://programmerbay.com/?p=6131 Program:

public class Main {
   static int count = 0;
    String search;
    int len;
    String[] str;
    Main(String[] s,String srch){
str = s;
search = srch;
len = s.length;
    }

    int call_me(int len) {

        if (len < 1) // base case
        {
            return count;
        } else if (str[len].equals(search)) {
            ++count;
        }
        return call_me(len - 1);
    }

    public static void main(String[] args) {
        Main obj;
        String[] str = {"this","is","a","string","and","a","sequence","of","a","string","arr"};
        obj = new Main(str,"a");
        System.out.println(obj.search+" occurred "+obj.call_me(obj.len-1)+" times");

    }
}

Output:

a occurred 3 times

]]>
https://programmerbay.com/program-to-count-a-particular-word-occurrence-in-a-string-without-using-loop/feed/ 0