Let's say we have a string we wish to split the string into 5 chars long without splitting individual words:
I am going to CUET
Now if we can split this in the following way:
I am
going
to
CUET
I write a code for this. First I break the string into words and save them into vector then take each word and check it's less than 5 or not. if not then I added the string into ans vector. Here is my code:
#include<bits/stdc++.h>
using namespace std;
vector<string>split(string txt)
{
    vector<string>result;
    string s="";
    for(int i=0; i<=txt.size();i++)
    {
        if( i<txt.size() and txt[i]!=' ')
            s+=txt[i];
        else
        {
            result.push_back(s);
            s="";
        }
    }
    return result;
}
int main()
{
    string str="I am going to CUET";
    int len=5;
    vector<string>result=split(str);
    vector<string>ans;
    int i=0;
    string s="";
    while(i<result.size())
    {
        if(i<result.size() and s.size()+result[i].size()<=len)
        {
            if(s=="") s+=result[i];
            else s+=" ",s+=result[i];
            i++;
        }
        else
        {
            ans.push_back(s);
            s="";
        }
    }
    if(s!="") ans.push_back(s);
    for(int i=0; i<ans.size();i++) cout<<ans[i]<<endl;
}
Is there any better solution than mine or any better solution without breaking the word first?
EDIT: Here is my solution without breaking the word first: http://ideone.com/IusYhE
