if ( start < str . length ( ) ) cout << endl ; to ignore final new line
#include <iostream> NEW_LINE using namespace std ; void divide ( string str , int n ) { if ( str . length ( ) % n != 0 ) { cout << " Invalid ▁ Input : ▁ String ▁ size " ; cout << " ▁ is ▁ not ▁ divisible ▁ by ▁ n " ; return ; } int parts = str . length ( ) / n ; int start = 0 ; while ( start < str . length ( ) ) { cout << str . substr ( start , parts ) << endl ; start += parts ; } } int main ( ) { string str = " a _ simple _ divide _ string _ quest " ; divide ( str , 4 ) ; }