Click here to Skip to main content
15,886,781 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all
I am trying to remove consecutive blank space by single blank space but getting error by this small line-
ch=''
This is what I have done till now-
C++
#include <iostream.h>
    #include <ctype.h>
    #include <conio.h>
    void main()
    {
        char ch;
        int count=0;
        ifstream in_stream;
        ofstream out_stream;
        clrscr();
        in_stream.open("A.txt");
        out_stream.open("B.txt");
        while (!in_stream.eof())
        {
            if(isspace(ch))
                   count++;
              
              if(count >= 2)
              {
                   ch='';
                   count = 0;
              }          
              else
              {
                  out_stream <<ch;
              }
              out_stream << ch;
        }
    }


Error is - Character constant must be one or two characters long
I am learning C++ in Turbo C++ as per our syllabus requirement.

Thank you in advance
Posted
Comments
Sergey Alexandrovich Kryukov 2-Aug-13 16:07pm    
Of course: what could be the "empty character"? Your ch='' makes no sense at all.
—SA
vishal deb 2-Aug-13 16:10pm    
Thank you for feedback.. then how can I able to replace the consecutive blank with one blank, guidance required.
Sergey Alexandrovich Kryukov 2-Aug-13 16:14pm    
I answered, please see. Replace double space with single space at the level of string, which could you either a line or your whole text. Repeat it until all consecutive blank space delegates are replaced (for example, it happens when the length of resulting string is no longer decreases).
—SA
vishal deb 2-Aug-13 16:18pm    
Thank you again, you said that "Replace double space with single space at the level of string" how to do this may I get some code snippet.
Sergey Alexandrovich Kryukov 2-Aug-13 16:21pm    
Finally, read my answer!
—SA

Please see my comment; it explains why your code is wrong, the whole idea of it. As this is C++, why not using std::string?
Please see:
http://www.cplusplus.com/reference/string/string/find/[^],
http://www.cplusplus.com/reference/string/string/replace/[^].

Are you getting the idea?

[EDIT]

Please see my second comment to the question explaining what to do with it. Find double blank space with std::string.find and replace it with std::string.replace.

—SA
 
Share this answer
 
v4
OK Thank you every one for considering my problem. I got the solution from http://cbsecsnip.in under question No. 15
Write a program that reads a text file and creates another file that is identical expect that every sequence of consecutive blank space is replaced by a single space.
here is the final code

C++
#include <fstream.h>
#include <iostream.h>
#include <ctype.h>
#include <conio.h>
void main()
{
    char ch;
    int count=0;
    ifstream in_stream;
    ofstream out_stream;
    clrscr();
    in_stream.open("A.txt");
    out_stream.open("B.txt");
    while (!in_stream.eof())
    {
       ch = (char)in_stream.get( );
        if(isspace(ch))
               count++;
          
          if(count >= 2)
          {
               ch=' ';
               count = 0;
          }          
          else
          {
              out_stream <<ch;
          }
    }
}</conio.h></ctype.h></iostream.h></fstream.h>


Working perfect
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900