Convert all substrings of length ‘k’ from base ‘b’ to decimal in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to convert all substrings of length ‘k’ from base ‘b’ to decimal.

For this we will be provided with a string of some certain length. Our task is to take the substrings from the given string of size ‘k’ and get it converted into the decimal numbers from being in base ‘b’.


 Live Demo

#include <bits/stdc++.h>
using namespace std;
//converting the substrings to decimals
int convert_substrings(string str, int k, int b){
   for (int i=0; i + k <= str.size(); i++){
      //getting the substring
      string sub = str.substr(i, k);
      //calculating the decimal equivalent
      int sum = 0, counter = 0;
      for (int i = sub.size() - 1; i >= 0; i--){
         sum = sum + (( - '0') * pow(b, counter));
      cout << sum << " ";
int main(){
   string str = "12212";
   int b = 3, k = 3;
   convert_substrings(str, b, k);
   return 0;


17 25 23
Published on 16-Jan-2020 11:14:20