Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.

Commit 6cc1ec2

Browse files
gauravdubey110abranhe
authored andcommitted
Create divide_string_in_n_equal_parts.cpp
This c++ program divides a string into n equal parts.
1 parent 3931b00 commit 6cc1ec2

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// C++ program to divide a string
2+
// in n equal parts
3+
#include<iostream>
4+
#include<string.h>
5+
6+
using namespace std;
7+
8+
class string_division
9+
{
10+
11+
// Function to print n equal parts of str
12+
public:
13+
void divide_String(char str[], int n)
14+
{
15+
16+
int str_size = strlen(str);
17+
int i;
18+
int part_size;
19+
20+
// Check if string can be divided in
21+
// n equal parts
22+
if (str_size % n != 0)
23+
{
24+
cout<<"Invalid Input: String size";
25+
cout<<" is not divisible by n";
26+
return;
27+
}
28+
29+
// Calculate the size of parts to
30+
// find the division points
31+
part_size = str_size / n;
32+
for (i = 0; i< str_size; i++)
33+
{
34+
if (i % part_size == 0)
35+
cout<<endl;
36+
cout<< str[i];
37+
}
38+
}
39+
};
40+
41+
// Driver code
42+
int main()
43+
{
44+
string_division g;
45+
46+
// length od string is 28
47+
char str[] = "a_simple_divide_string_quest";
48+
49+
// Print 4 equal parts of the string
50+
g.divide_String(str, 4);
51+
return 0;
52+
}
53+

0 commit comments

Comments
 (0)