Find the count of substrings in alphabetic order | CPP to find the number of substrings in alphabetical order ; Function to find number of substrings ; Iterate over string length ; if any two chars are in alphabetic order ; find next char not in order ; return the result ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSubstringCount ( string str ) { int result = 0 ; int n = str . size ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] + 1 == str [ i + 1 ] ) { result ++ ; while ( str [ i ] + 1 == str [ i + 1 ] ) { i ++ ; } } } return result ; } int main ( ) { string str = " alphabet " ; cout << findSubstringCount ( str ) << endl ; return 0 ; }