Check if all occurrences of a character appear together | C ++ program to find if all occurrences of a character appear together in a string . ; To indicate if one or more occurrences of ' c ' are seen or not . ; Traverse given string ; If current character is same as c , we first check if c is already seen . ; If this is very first appearance of c , we traverse all consecutive occurrences . ; To indicate that character is seen once . ; Driver program
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; bool checkIfAllTogether ( string s , char c ) { bool oneSeen = false ; int i = 0 , n = s . length ( ) ; while ( i < n ) { if ( s [ i ] == c ) { if ( oneSeen == true ) return false ; while ( i < n && s [ i ] == c ) i ++ ; oneSeen = true ; } else i ++ ; } return true ; } int main ( ) { string s = "110029" ; if ( checkIfAllTogether ( s , '1' ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }