
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert all 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
- Related Articles
- Convert from any base to decimal and vice versa in C++
- Count all Prime Length Palindromic Substrings in C++
- Find K-Length Substrings With No Repeated Characters in Python
- Maximum count of substrings of length K consisting of same characters in C++
- Find all possible substrings after deleting k characters in Python
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k in C++
- Program to check whether all palindromic substrings are of odd length or not in Python
- Print all increasing sequences of length k from first n natural numbers in C++
- Java Program to convert from decimal to binary
- Java Program to convert from decimal to hexadecimal
- Print all possible strings of length k that can be formed from a set of n characters in C++
- Map an integer from decimal base to hexadecimal with custom mapping JavaScript
- Program to find sum of beauty of all substrings in Python
- Convert Decimal to Binary in Java
- Convert the following temperature to Celsius scale:(a) 300 K(b) 573 K

Advertisements