how would i remove all the white space from an istream and replace them with "-" before producing an ostream?

Member Avatar for iamthwee

maybe

string rem ( string t )
{
   int s_t = t.length();

   for ( int i = 0; i < s_t; i++ )
   {
      if ( t[i] == ' ' || t[i] == '\n' || t[i] == '\t' )
      {
         t[i] = '-';
      }
   }
   return t;
}

or maybe even:

int main(){
    ifstream in("in.txt");
    ofstream out("out.txt");
    string str = "";
    while (in >> str) out << str << "-";
 }

No errorchecking, but it's to get the basic idea

>>while (in >> str) out << str << "-";

What??? That will not replace white spaces with '-' character.

while( in >> str)
{
    for(int i = 0; i < str.length(); i++)
    {
        if( isspace(str[i]) )
          str[i] = '_';
    }
    out << str << "\n";
}


What??? That will not replace white spaces with '-' character.

Why not? Am I missing the point completely or what?

input:

hello 			test test hello
1 2 3

output:

hello-test-test-hello-1-2-3-

Voila, all whitespace has been replaced with "-". That's what the OP wanted right?

And what is your code supposed to do? isspace will never be true because of the way your read in your string ( in >> str )
Perhaps you meant:

while(getline(in,str))
{
    for(int i = 0; i < str.length(); i++)
    {
        if( isspace(str[i]) )
          str[i] = '-';
    }
    out << str << "\n";
}

?

Then the output would be:

hello----test-test-hello
1-2-3

Which could also be what the OP wants :)

commented: My bad -- you are right :) +36

Sorry, I misunderstood your code.

>>And what is your code supposed to do?
Nothing -- as you well stated. My post was just a brain fart.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.