Program to implement string handling functions
Here we are trying to check the occurrence of character in string
import java.io.*; class Occurences { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s, s1; System.out.print("Enter String:: "); s = br.readLine(); System.out.print("Enter character to check occurences:: "); s1 = br.readLine(); char c = s1.charAt(0); // Reading only first character int n = s.length(); int i, cnt = 0; char c1; for (i = 0; i < n; i++) { c1 = s.charAt(i); if (c1 == c) cnt++; } if (c == ' ' || c == '\t') System.out.println("\nWhite space occurs " + cnt + " times"); else System.out.println("\n" + c + " occurs " + cnt + " times"); } }
OUTPUT::
Enter String::Hello World Enter character to check occurences::o
o occurs 2 times