-1

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

akid
  • 23
  • 2
  • 6
  • 2
    First please read [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Then you might want to search for algorithms about *word wrap*. – Some programmer dude Mar 26 '17 at 09:38
  • How do you decide to keep `I` and `am` together? – Code-Apprentice Mar 26 '17 at 09:58
  • @Code-Apprentice "wish to split the string into 5 chars long" – Disillusioned Mar 26 '17 at 10:10
  • akid, What do you if one of your words is longer than 5 chars? E.g. "problem". This is an important question you seem not to have thought about and has some impact on your solution. That aside, you'll want to look for an approach that **doesn't** split your processing into phases as you have described. Ideal solution is to read input until you're satisfied you have everything you need for the current output line. The trick to getting that to work requires ***NOT*** building up output string char-by-char, but rather tracking positions and then copying sub-strings based on what you tracked. – Disillusioned Mar 26 '17 at 10:18

1 Answers1

0

I'm not sure that I understood your "<=5 long" check, but here is my view:

#include <iostream>
#include <vector>

void main()
{
    std::string szSentence = "I am going to CUET";
    std::vector<std::string> vWords;

    while(szSentence.length() > 0)
    {
        if (szSentence.substr(0, szSentence.find(" ")).length() >= 5)
            vWords.push_back(szSentence.substr(0, szSentence.find(" ")));

        if (szSentence.find(" ") != std::string::npos)
            szSentence = szSentence.substr(szSentence.find(" ")+1);
        else
            szSentence = "";
    }   
}