Check if the characters of a given string are in alphabetical order | C ++ implementation of above approach ; Function that checks whether the string is in alphabetical order or not ; length of the string ; create a character array of the length of the string ; assign the string to character array ; sort the character array ; check if the character array is equal to the string or not ; Driver code ; check whether the string is in alphabetical order or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isAlphabaticOrder ( string s ) { int n = s . length ( ) ; char c [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = s [ i ] ; } sort ( c , c + n ) ; for ( int i = 0 ; i < n ; i ++ ) if ( c [ i ] != s [ i ] ) return false ; return true ; } int main ( ) { string s = " aabbbcc " ; if ( isAlphabaticOrder ( s ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }