0

I'm still a bit of a newbie and have what is probably a minor issue, but for some reason I can't wrap my mind around it.
I have a program that does a whole bunch of stuff to text from an input file. The input looks like this:

MArch    9,    1971.  SPace the finAL      fronitIER!
    "TheSe aRe      the voyAges     of the    staRship
    enTERprIse"?

With the current result looking like:

March    9,    1971.  Space the final      fronitier!
        "These are      the voyages     of the    starship
        enterprise"?

But what I need to do is get rid of all of the spaces and format it so....

March 9, 1971.  Space the final fronitier!
"These are the voyages of the starship
enterprise"?

If I read in the string one word at a time, I can easily concatenate the ouput , ( cout <<text+' ')
but the way that I read in the input, I used get(ch), which isn't playing nice for me so far.
So how do I concatenate the output?

Any help would be greatly appreciated and here is the code for this segment of my program...

 input.get(ch);
    if (ch=='"')
    {
        new_sentence=true;
        cout << (char)ch;
    }
    else
    {
        new_sentence=false;
        cout << (char)toupper(ch);
    }
    while(input.get(ch))
    {
   if(isalpha(ch))
   {
      if( new_sentence )
    cout << (char)toupper(ch);
      else 
    cout << (char)tolower(ch);
      new_sentence = false;
   }
   else
   {
      if( ch == '.' ||  ch == '!' ||  ch == '?') 
          new_sentence = true;
      cout << (char)ch;
   }

Thanks in advance!
Robert

2
Contributors
1
Reply
17
Views
3 Years
Discussion Span
Last Post by deceptikon
1

It really would be best to use word granularity rather than character granularity. Collect all of the words in a list and then you can use a simply greedy word wrapping algorithm to display them in a bounded area. For example:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <utility>
#include <vector>

using namespace std;

namespace {
    const string::size_type line_width = 40;
}

int main()
{
    ifstream in("test.txt");

    if (in) {
        vector<string> words;
        string line;

        // Build the word list
        //
        while (getline(in, line)) {
            auto x = vector<string>(
                sregex_token_iterator(line.begin(), line.end(), regex("\\s+"), -1), 
                sregex_token_iterator());

            x.erase(remove_if(x.begin(), x.end(), [](const string& s) { return s.empty(); }), x.end());

            for (auto& word : x) {
                words.push_back(move(word));
            }
        }

        // Display each wrapped line
        //
        auto remaining = line_width;

        cout << string(line_width, '#') << '\n';

        for (const auto& word : words) {
            auto width = word.size() + 1;
            bool wrap = width > remaining;

            cout << (wrap ? "\n" : "") << word << ' ';
            remaining = (wrap ? line_width : remaining) - width;
        }

        cout << '\n' << string(line_width, '#') << '\n';
    }
}

How you collect the words is up to you and generally straightforward, so I did something concise. The important part is the word wrapping algorithm.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.