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.
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.
Why are you resetting count and printing a space every time you encounter a second space?
Name:
Anonymous2012-11-08 1:39
Using procedural, application-programming languages for this type of work is time consuming and un-needed, use a high level scripting language for this type of work. If you have to use c++ thou, use strategically placed print statements, and make sure that the code is EXACTLY correct, read it in great detail.
Name:
Anonymous2012-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.