Character B follows A JAVA - EASY - PART005

 Character B follows A

Given a string S and two characters A, B the program must print the number of occurrences where A is followed by B.

***********************************************************************************

import java.util.*;

public class Hello

{

public static void main(String args[])

{

Scanner in=new Scanner(System.in);

String str=in.nextLine();

char c=in.next().charAt(0);

char c1=in.next().charAt(0);

char[] ch=str.toCharArray();

int count=0;

for(int i=0;i<ch.length-1;i++)

{

if(ch[i]==c && ch[i+1]==c1)

{

count++;

}

}

System.out.println(count);

}

}


***********************************************************************************

Comments

Popular Posts