Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C VC10.0
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:
 
#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:
#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 8-Apr-11 6:22am
Comments
Albert Holguin at 8-Apr-11 11:36am
   
Please put one question per Q&A post, it'll make it easier for everyone.
StupidSteve at 8-Apr-11 11:38am
   
Okay, THX for your comment and I am also a newcomer to this website:)
Albert Holguin at 8-Apr-11 11:40am
   
Great, welcome to CodeProject! you'll find a lot of helpful ppl here... :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Ive done your homework for you:
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.
  Permalink  
Comments
StupidSteve at 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 at 8-Apr-11 12:45pm
   
You have to include:

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

i assumed you can use the dev-studio help manual. that example don't work in pure c (only c++).
Regards.
StupidSteve at 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!~
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
while ((c=getchar())!=EOF)
{
    if ((c=getchar())!=' ')
    {
      ...
    }
    else ...;
}
  Permalink  
Comments
StupidSteve at 8-Apr-11 12:00pm
   
O! I finally understand that! It is such a mistake...
 
Thank you so much!!!!
Albert Holguin at 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 at 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 at 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 at 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.
StupidSteve at 8-Apr-11 13:14pm
   
yes you are right!
i should type () firstly before typing anything between them---- so it makes this kind of typo happens less often~
 
Thanks for your advice!

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

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 8 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100