
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Substrings of Length K from Base B to Decimal in C++
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
#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
Advertisements