Convert all substrings of length ' k ' from base ' b ' to decimal | Simple C ++ program to convert all substrings from decimal to given base . ; Saving substring in sub ; Evaluating decimal for current substring and printing it . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int substringConversions ( string str , int k , int b ) { for ( int i = 0 ; i + k <= str . size ( ) ; i ++ ) { string sub = str . substr ( i , k ) ; int sum = 0 , counter = 0 ; for ( int i = sub . size ( ) - 1 ; i >= 0 ; i -- ) { sum = sum + ( ( sub . at ( i ) - '0' ) * pow ( b , counter ) ) ; counter ++ ; } cout << sum << " ▁ " ; } } int main ( ) { string str = "12212" ; int b = 3 , k = 3 ; substringConversions ( str , b , k ) ; return 0 ; }