Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I am a totally newcomer to C and I am currently reading K&R's The C Programming Language.

Today I try to solve 2 simple exercises from this book and I found something wrong with my code----but I don't know why. Could anybody help me about them? THX alot!

PS, I am using Microsoft's visual studio 2010 to code.

Q1: write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

my code:

C#
#include <stdio.h>
#include <stdlib.h>
#define OUT 0
#define IN 1
main()
{
    int c, state;

    state=IN;

    printf("This is a minyfunction designed to omit blanks you typed in into one blank\n");
    printf("Please type in 'Control + Z ' in a new line when you have finished typing\n");


    while ((c=getchar())!=EOF)
    {

        if ((c=getchar())!=' ')
        {
            if (state==OUT)
            {
                printf(" ");
                state=IN;
                putchar(c);
            }
            else putchar(c);
        }
        else state=OUT;
    }
    system("Pause");

}


The exchange from several blanks into one blank works out okay, but for every two characters I type in, one of them will be omitted and I don't know why...


Q2: Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\.

my code:
C#
#include <stdio.h>

main()
{
    int c;

    while ((c=getchar()!=EOF))
    {
        if (c==' ')
        {
            printf("\\n");
        }
        else if (c=='\t')
        {
            printf("\\t");
        }
        else if (c=='\n')
        {
            printf("\\n");
        }
        else if (c=='\\')
        {
            printf("\\");
        }
        else
        putchar(c);
    }

    system("Pause");
}


The problem is that the output characters on screen all turn to be signs of smiling faces...I don't know why these characters become similing faces and I hope anybody could help me to revise these code to make it work out okay.

Sorry about my really poor English----I am not a native speaker...

THX for your help!
Posted
Comments
Albert Holguin 8-Apr-11 11:36am    
Please put one question per Q&A post, it'll make it easier for everyone.
StupidSteve 8-Apr-11 11:38am    
Okay, THX for your comment and I am also a newcomer to this website:)
Albert Holguin 8-Apr-11 11:40am    
Great, welcome to CodeProject! you'll find a lot of helpful ppl here... :)

Ive done your homework for you:
C#
void main()
{
  struct
  {
    static int space(TCHAR c){ return (' '==c)||('\t'==c)||('\r'==c)||('\n'==c); }
    static int escape(TCHAR c){ return ('\t'==c)||('\b'==c)||('\\'==c); }
  }      is;
  TCHAR  c;
  int    s = 0;
  // Q1: solution
  while('\x1a'/*ctrl+z*/!=(c=_gettch()))
  {
    if(is.space(c))
    {
      if(0==s++) _puttchar(' ');
    }
    else
    {
      _puttchar(c);
      s = 0;
    }
  }
  // Q2: solution
  while('\x1a'/*ctrl+z*/!=(c=_gettch()))
  {
    if(is.escape(c))
    {
      _puttchar('\\');
      _puttchar('\\');
    }
    else
    {
      _puttchar(c);
    }
  }
}


Regards.
 
Share this answer
 
Comments
StupidSteve 8-Apr-11 12:05pm    
Thank you very much...but... i cannot read your code and i copied them into VC2010 and several errors had been detected out...

Thank you all the same~!
mbue 8-Apr-11 12:45pm    
You have to include:

#include <tchar.h>
#include <conio.h>
</conio.h></tchar.h>

i assumed you can use the dev-studio help manual. that example don't work in pure c (only c++).
Regards.
StupidSteve 8-Apr-11 13:08pm    
okay~I finally understand your points~
in fact i was thinking that perhaps your were programming in kind of German C:)
BTW, I like German VERY MUCH!~
First Question:

In this loop, you're removing every other character by checking for EOF and following that up with an if statement that calls getchar() as well, you should only call getchar() once per loop iteration:
C++
while ((c=getchar())!=EOF)
{
    if ((c=getchar())!=' ')
    {
      ...
    }
    else ...;
}
 
Share this answer
 
Comments
StupidSteve 8-Apr-11 12:00pm    
O! I finally understand that! It is such a mistake...

Thank you so much!!!!
Albert Holguin 8-Apr-11 13:03pm    
You're welcome... glad you have your stuff working... I was off to lunch so didn't have a chance to look at your second question...
StupidSteve 8-Apr-11 12:02pm    
I rewrite it into:

while ((c=getchar())!=EOF)
{

if (c!=' ')
{
if (state==OUT)
{
printf(" ");
state=IN;
putchar(c);
}
else putchar(c);
}
else state=OUT;
}
system("Pause");

}


and the first program works out okay~ finally ~
StupidSteve 8-Apr-11 12:30pm    
And I finally find out what's wrong with my 2nd program...

it should be :
...
while ((c=getchar())!=EOF)
...

so it's a typo... :)
Albert Holguin 8-Apr-11 13:06pm    
oh yep... you should enclose chunks of code where order may be ambiguous, even in the event that the order gets evaluated correctly by the compiler, it'll make your code more readable to do so.

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