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

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

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’.

## Example

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 + ((sub.at(i) - '0') * pow(b, counter));
counter++;
}
cout << sum << " ";
}
}
int main(){
string str = "12212";
int b = 3, k = 3;
convert_substrings(str, b, k);
return 0;
}

## Output

17 25 23