Count strings with consonants and vowels at alternate position | Java implementation of above approach ; Function to find the count of strings ; Variable to store the final result ; Loop iterating through string ; If ' $ ' is present at the even position in the string ; ' sum ' is multiplied by 21 ; If ' $ ' is present at the odd position in the string ; ' sum ' is multiplied by 5 ; Driver code ; Let the string ' str ' be s$$e$ ; Print result
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int countStrings ( String s ) { int sum = 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) == ' $ ' ) sum *= 21 ; else if ( s . charAt ( i ) == ' $ ' ) sum *= 5 ; } return sum ; } public static void main ( String args [ ] ) { String str = " s $ $ e $ " ; System . out . println ( countStrings ( str ) ) ; } }