funcction

Write a function to with an input file and output file as arguments to edit the input file and replace every string of two or more consecutive blanks with a single blank.
's/ +/ /g'
^ since that isn't really searchable, let me just say ne555 is referring to regular expressions (regex).
https://en.wikipedia.org/wiki/Regular_expression

Try it out at:
https://regexr.com/

Regex is available in C++:
http://www.cplusplus.com/reference/regex/
http://www.cplusplus.com/reference/regex/regex_match/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cctype>
#include <iterator>

template <typename InputIterator, 
          typename OutputIterator>
void collapse_spaces(InputIterator begin, InputIterator end,
                     OutputIterator dest)
{
    bool previously_had_space = false;
    for (auto it = begin; it != end; ++it)
    {
       if (!(std::isspace(*it) && previously_had_space))
            *dest++ = *it;           
       
       previously_had_space = std::isspace(*it);
    }
}

int main()
{
    std::cin >> std::noskipws;
    collapse_spaces(std::istream_iterator<char>{std::cin}, {}, 
                    std::ostream_iterator<char>{std::cout});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

const char SPACE = ' ';

//==========================================================

string doline( string sentence )
{
   string result, item;
   bool first = true;

   stringstream ss( sentence );

   while ( ss >> item )
   {
      if ( !first ) result += SPACE;
      first = false;
      result += item;
   }

   return result;
}

//==========================================================

int main()
{
   stringstream in( "...    that this    nation, under    God,    shall        \n"
                    "    have a    new birth of    freedom  -  and that        \n"
                    "     government of     the people,    by the people,   for\n"
                    "  the    people,   shall not   perish from  the earth.    \n" );
   string line;
   while ( getline( in, line ) ) cout << doline( line ) << '\n';
}

//========================================================== 


... that this nation, under God, shall
have a new birth of freedom - and that
government of the people, by the people, for
the people, shall not perish from the earth.
1
2
3
4
5
6
7
8
9
10
11
// compress_blanks.d

void main(string[] args) {

    import std.regex;
    import std.file;

    static ws = ctRegex!`[^\S\r\n]+`;

    args[2].write(args[1].readText.replaceAll(ws, " "));
}

in.txt
Lorem    ipsum dolor sit amet,    consectetur adipiscing elit, 
sed    do   eiusmod  tempor incididunt    ut labore et dolore 
magna aliqua.  Integer malesuada   nunc vel   risus   commodo 
viverra.  Arcu  bibendum at varius   vel pharetra vel  turpis. 

out.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt ut labore et dolore 
magna aliqua. Integer malesuada nunc vel risus commodo 
viverra. Arcu bibendum at varius vel pharetra vel turpis. 
^ since that isn't really searchable, let me just say ne555 is referring to regular expressions (regex).
Why shouldn't it be?

https://www.startpage.com/do/dsearch?query=replace+two+or+more+consecutive+blanks+with+a+single+blank&cat=web&pl=ext-ff&language=english
I was referring to ne555's post.
Sorry I misunderstood.
Topic archived. No new replies allowed.