Thread: Replacing string of blanks

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    69

    Replacing string of blanks

    Hello,
    Code:
    #include<stdio.h>
    #define x 'a'
    
    
    int main()
    {
      int c,y;
    
    
      y=x;
    
    
      while((c=getchar())!=EOF)
      {
        if(c==' ')
        {
          if(y!=' ')
            putchar(c);
        }
        else
          putchar(c);
    
    
       y=c;
      }
    return 0;
    }

    In the above code in 2nd line why the compiler shows error if we use ' ' in stead of 'a', and please let me know the below lines
    if(y!=' ') putchar(c);
    how can these lines replace multi spaces by a single space.

    what is y = c;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Shankar k
    In the above code in 2nd line why the compiler shows error if we use ' ' in stead of 'a',
    What is the error message? I replaced 'a' with ' ' on that line and your program compiled without even a warning on gcc 4.8.4

    By the way, it is conventional to name macros with fully uppercase names, so x should have been X. Actually, X is a poor name for a macro like this because it is not descriptive. Perhaps INITIAL_VALUE would be better.

    Quote Originally Posted by Shankar k
    and please let me know the below lines
    if(y!=' ') putchar(c);
    how can these lines replace multi spaces by a single space.
    Use a loop with a getchar call.

    Quote Originally Posted by Shankar k
    what is y = c;
    Think about what those variables are for, then rename them to have better (i.e., more descriptive) names. From there, the reason for y = c should become evident.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    69
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Entab - replacing blanks with tabs and blanks
    By thames in forum C Programming
    Replies: 3
    Last Post: 10-20-2012, 03:11 PM
  2. Removing blanks from beginning of string
    By jwroblewski44 in forum C Programming
    Replies: 3
    Last Post: 08-25-2012, 05:04 PM
  3. Replies: 7
    Last Post: 03-17-2012, 09:36 PM
  4. Replacing character with the string
    By gammie in forum C Programming
    Replies: 2
    Last Post: 06-20-2011, 03:02 PM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM