- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Divide a string in N equal parts in C++ Program
In this tutorial, we are going to write a program that divides the given string into N equal parts.
If we can't divide the string into N equal parts, then print the same thing. Let's see the steps to solve the problem.
Initialize the string and N.
Find the length of the string using the size method.
Check whether the string can be divided into N parts or not.
If string can't divide into N equal parts, then print a message.
Else iterate through the string and print each part.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; void divideTheString(string str, int n) { int str_length = str.size(); if (str_length % n != 0) { cout << "Can't divide string into equal parts" << endl; return; } int part_size = str_length / n; for (int i = 0; i < str_length; i++) { if (i != 0 && i % part_size == 0) { cout << endl; } cout << str[i]; } cout << endl; } int main() { string str = "abcdefghij"; divideTheString(str, 5); return 0; }
Output
If you execute the above program, then you will get the following result.
ab cd ef gh ij
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Java Program to divide a string in 'N' equal parts
- Divide a string into n equal parts - JavaScript
- Divide a number into two parts in C++ Program
- Split a string in equal parts (grouper in Python)
- Divide number into two parts divisible by given numbers in C++ Program
- Divide a big number into two parts that differ by k in C++ Program
- Three Equal Parts in C++
- Check if a line at 45 degree can divide the plane into two equal weight parts in C++
- Split string into equal parts JavaScript
- Divide large number represented as string in C++ Program
- Count number of ways to divide a number in parts in C++
- Count digits in given number N which divide N in C++
- C Program to delete n characters in a given string
- Draw an angle of measure ( 153^{circ} ) and divide it into four equal parts.
- C# Program to split parts in a Windows directory

Advertisements