Consecutive sequenced numbers in a string | CPP Program to check whether a string contains consecutive sequential numbers or not ; function to check consecutive sequential number ; variable to store starting number ; length of the input string ; find the number till half of the string ; new string containing the starting substring of input string ; converting starting substring into number ; backing up the starting number in start ; while loop until the new_string is smaller than input string ; next number ; concatenate the next number ; check if new string becomes equal to input string ; if string doesn 't contains consecutive numbers ; Driver 's Code
#include <iostream> NEW_LINE using namespace std ; int isConsecutive ( string str ) { int start ; int length = str . size ( ) ; for ( int i = 0 ; i < length / 2 ; i ++ ) { string new_str = str . substr ( 0 , i + 1 ) ; int num = atoi ( new_str . c_str ( ) ) ; start = num ; while ( new_str . size ( ) < length ) { num ++ ; new_str = new_str + to_string ( num ) ; } if ( new_str == str ) return start ; } return -1 ; } int main ( ) { string str = "99100" ; cout << " String : ▁ " << str << endl ; int start = isConsecutive ( str ) ; if ( start != -1 ) cout << " Yes ▁ STRNEWLINE " << start << endl ; else cout << " No " << endl ; string str1 = "121315" ; cout << " String : " start = isConsecutive ( str1 ) ; if ( start != -1 ) cout << " Yes ▁ STRNEWLINE " << start << endl ; else cout << " No " << endl ; return 0 ; }