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;
}

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.