Divide a string in N equal parts | C ++ program to divide a string in n equal parts ; Function to print n equal parts of str ; Check if string can be divided in n equal parts ; Calculate the size of parts to find the division points ; Driver code ; length od string is 28 ; Print 4 equal parts of the string
#include <iostream> NEW_LINE #include <string.h> NEW_LINE using namespace std ; class gfg { public : void divideString ( char str [ ] , int n ) { int str_size = strlen ( str ) ; int i ; int part_size ; if ( str_size % n != 0 ) { cout << " Invalid ▁ Input : ▁ String ▁ size " ; cout << " ▁ is ▁ not ▁ divisible ▁ by ▁ n " ; return ; } part_size = str_size / n ; for ( i = 0 ; i < str_size ; i ++ ) { if ( i % part_size == 0 ) cout << endl ; cout << str [ i ] ; } } } ; int main ( ) { gfg g ; char str [ ] = " a _ simple _ divide _ string _ quest " ; g . divideString ( str , 4 ) ; return 0 ; }