i found out how to get each individual word from a text file. Now i am trying to limit each line to a certain number of characters. If the line limit is 10 and the word "string" starts at position 8, i need to make a newline and put "string" on the new line. I want to count characters to control the limit, however i do not know how to count the characters given that i got the words from a text file.

Here is my code so far:

int rearrange(int lineLength, istream& inf, ostream& outf)
{
    int charInLine = 0;
    const int maxLength = 10000;

    getWord(inf, outf);

    for(int i = 0; i < maxLength; i++)
    {

    }


    return 0;
}

int getWord(istream &inf, ostream &outf)
{
    char sentence[100000];

    while(!inf.eof())
    {
        inf >> sentence;
        outf << sentence << " ";
    }

    return 0;
}

Dani AI

Generated

A simple, robust approach is to read the input line-by-line (so original blank lines / paragraph breaks can be preserved), split each input line into words with an istringstream, and maintain a running counter for the current output line length. Before printing each word, check whether adding it (plus a space if not at line start) would exceed the limit; if so, emit a newline first. This avoids splitting words and avoids the pitfalls of while(!inf.eof()) and large fixed char buffers.

#include <string>
#include <sstream>

int rearrange(int lineLength, std::istream &inf, std::ostream &outf)
{
    std::string line, word;
    int current = 0; // chars on current output line

    while (std::getline(inf, line)) {
        if (!line.empty() && line.back() == '\r') line.pop_back(); // trim CR on Windows files

        if (line.empty()) {                 // preserve paragraph breaks
            if (current != 0) { outf << '\n'; current = 0; }
            outf << '\n';
            continue;
        }

        std::istringstream iss(line);
        while (iss >> word) {
            int wlen = static_cast<int>(word.size());
            int sep = (current == 0) ? 0 : 1; // space needed if not at line start

            if (current + sep + wlen > lineLength) {
                outf << '\n' << word;
                current = wlen;
            } else {
                if (sep) outf << ' ';
                outf << word;
                current += sep + wlen;
            }
        }
    }

    if (current != 0) outf << '\n';
    return 0;
}

Notes and edge cases: punctuation counts as part of a word with this method; if you want to keep trailing punctuation from wrapping (e.g., treat "word." separately), strip/reattach punctuation tokens when deciding wrap points. For words longer than the column limit you can either place them alone on a line or implement a hyphenation/splitting routine (a simple chunk-and-hyphen scheme works but may split syllables awkwardly). Trim any trailing '\r' from getline to handle CRLF files. Also be cautious with UTF-8: std::string::size() gives bytes, not display columns, so multi-byte characters can break visual alignment.

This builds on ideas already suggested by and (use getline/splitting) and follows 's advice to avoid eof-based loops and switch to std::string rather than huge char arrays. Test with blank lines, multiple spaces, long tokens, and punctuation to confirm behavior.

Recommended Answers

All 10 Replies

Hi, and Welcome to Daniweb
Your function getWord() is already posting the contents over into the file.. Maybe you should be looking towards 2 options.

1) getWord() returns a string, and you decide if the string overfills your limit. If yes move into the next line or else.. let it stay on the same line..

2) Move all the processing into getWord() ( as you are processing the whole file in the single function, and maybe change the name of the function.) and devise a mechanism to store the number of characters in that line..

Give us ideas on how you wish to implement it.. And we all will solve the problem as a UNIT :)

I have decided to put all my processes into my int rearrange function.

int rearrange(int lineLength, istream& inf, ostream& outf)
{
    char sentence[100000];
    int counter = 0;

    while(!inf.eof())
    {
        inf >> sentence;
        outf << sentence << " ";
    }

    return 0;

}

Am i wrong to say that "sentence" in my code is a word that gets put into the output? that is why i'm confused on how to count characters. the first step that you listed sounds like the more efficent route to take now that im pulling words out of the input. i want to set a line length at 40. after 40 characters, there should be a new line.

To do this i want to :
read the input
find the words (exclude white space)
if the words on a line exceed 40 total characters, make a new line
put this all in the out file

I feel like i need to do this inbetween

            inf >> sentence;

            outf << sentence << " ";

There needs to be a counter that incrementally increases as the words get read by the # of characters in the word.
The function needs to check the word length before increasing the counter because i dont want words to get split.

The one part about the string overflowing the limit confuses me. Is it possible to add string lengths to a counter to keep track? or do i have to do it with characters.

This is what i came up with:

int rearrange(int lineLength, istream& inf, ostream& outf)
{
    char sentence[100000];
    int counter = 0;

    while(!inf.eof())
    {
        inf >> sentence;
        counter += strlen(sentence);
        if(counter > lineLength)
        {
            outf << "\n";
            counter = 0;
        }
        outf << sentence << " ";
    }
    return 0;
}

i think it works pretty well... but now i have to handle hyphens, periods, question marks and newline characters. can anyone pose a few questions that i can think about to get started on these?

One question :

Does the method i use to pull out strings from the text file see new line characters at all?

No It doesn't see new line characters. It only is retrieving words from the file.. There by considers all whitespace or "\n" characters to be ommited.

erm im newbie in c++, but what i do to count characters is by passing the words to string by using getline.
im not sure if this is a good code, but at least it works for me.

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main()
{

    ifstream inputFile;
    inputFile.open("asd.txt");

    string word;
    getline(inputFile, word);

    //then use loop to count whitespace

    int index = 0, acc = 1;
    while(word[index] != '\0')
    {
        if(word[index] == ' ')
        acc++;
        index++;
    }

    vector<int> ptr(acc, 0);
    int a = 0;

    for(int count = 0; count < acc; count++)
    {

        while(word[a] != '\0' && word[a] != ' ')
        {
            ptr[count] += 1;
           a++;
        }
        a+= 1;
    }

    for(int count = 0 ; count  < acc; count++)
    {
        cout<<ptr[count]<<endl;
    }

}

thats what i do, correct me if im wrong cause im newbie :D

char sentence[100000];

How many words do you know are 100000 characters long? Wouldn't a shorter 'sentence' be better? And if you only want words, would a variable named word be less confusing than sentence? ;o)

Also, while(!inf.eof()) -- see this

yeah at first i was reading whole lines of input at a time so i named it sentence and wanted to never be limited. now that i shortened it to each word i will be changing it up! thanks

Right now i am trying to figure out how to be able to read newline characters while keeping the structure of my code. does anyone have any ideas on how to do that?

instead of inf>>sentence why don't your read a line from the file (getline()) should do great. Split the line into words . And then let the process flow logic continue. This way you would have to only add the "get a line and split part into your code" :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.