Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Intro C++

Name: Anonymous 2012-11-07 22:14

I'm trying to write a program that edits a text file to replace all consecutive spaces with a single space.  The editing of the spaces is what's giving me problems.

}

while(! fin.eof())
    {
        fin.get(next);

         if(isspace(next))
             count = count + 1;
        
         if(isspace(next) && count >= 2)
         {
            fout << ' ';
            count = 0;
         }
       
         else
         {
            fout << next;   
            count = 0;
         }
    }
    fin.close( );
    fout.close( );
       
    }

Basically, I'm trying to add 1 every time a space is encountered as some sort of comparison, when it's = 2 it gets replaced with single space.  So far it outputs the text unedited.  Any help would be appreciated.

Name: Anonymous 2012-11-08 2:41

the more simple way to handle the problem is to build a function that breaks up the string from the file into an array of words.

e.g.

array words(inputstring):
  str word = emptystring
  array words = emptyarray
  for character in string:
    if character is not space:
      word += character
    else:
      array.push(word)
  return words

string join_nicely(words)]
  string the_output
  for word in words:
    if word is not space:
      the_output += (word + space)
  return the_output

This all handles quadruple spaces as well and yes using C++ for this will be tedious - but should be a nice exercise to get you started.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List