Changing space to nothing

Mar 28, 2017 at 12:10am
I am reading in from a file and writing it to another file without white spaces. All i need to figure out is how to remove spaces. For instance,
INPUT:Hello how are you
OUTPUT:Hellohowareyou
Mar 28, 2017 at 12:45am
This site will help with modifying the strings: http://www.cplusplus.com/reference/string/string/
Theirs 2 easy ways of doing this both similar. 1. Use find and look for spaces and when you find the position of a space erase it. Put this in a loop that looks for and removes spaces till it runs through the entire string without finding any spaces. 2. Access it as you would an array. stringName[] and look for spaces that way and remove them.
Mar 28, 2017 at 2:05am
OP: or you could use std::regex:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::string target = "the dog jumped over the fence";
    std::regex reg1(" ");
    target = std::regex_replace(target, reg1, "");
    std::cout << target << "\n";//prints "thedogjumpedoverthefence"
}
Mar 28, 2017 at 2:21am
> I am reading in from a file and writing it to another file without white spaces.
> All i need to figure out is how to remove spaces.

By default, formatted input skips white space. So, you don't have to do anything more than:
read characters from the input stream with >> and write out the characters that were read to the output stream.

1
2
3
// copy infile to outfile without white spaces
char c ;
while( infile >> c ) outfile << c ;

http://coliru.stacked-crooked.com/a/9b98a1d94b4ce2e8
Topic archived. No new replies allowed.