2

This is for homework . Must use only getchar and putchar

int main(void) {
int pch; //first
int ch; //second


while(1){
    pch=getchar();
    ch=getchar();


    if(((pch>='A' && pch<='Z')) && ((ch>='A' && ch<='Z'))){
        putchar(ch);
        putchar(pch);
    }

    if((pch>='A' && pch<='Z') && ch=='\n') putchar(pch);
    if(pch=='\n' || ch=='\n') break;
}
return EXIT_SUCCESS;
}

I need to swap chars using getchar and putchar . For exemple PARIS APIRS

And it works , but i need to hit ENTER two times when i even number of letters 4,6,8... . How do i eliminate this behavior ? Is there some way to kill \n for getchar

  • 1
    I typed PAR, hit one time enter and got APR. Is that what you want? If not, what do you want? Same with PARA. I hit enter once and got APAR. – gsamaras Sep 23 '14 at 15:33
  • 2
    you can make `if(pch == '\n') break;` after `pch=getchar();` – mch Sep 23 '14 at 15:40
  • mch it works , but can you explain to me how did come up with that. – Pedja Aleksic Sep 23 '14 at 16:04
  • @PedjaAleksic, if you read my answer, you will understand. mch says, if pch is a newline, then I break the loop. First (s)he has used `getchar()` to read into `pch`. – gsamaras Sep 23 '14 at 16:10
  • I know Samaras. I have two "getchar()" in a row (lines 12 and 13). So if you enter an even number of characters, the program will end up waiting when the last character is read, since there is that second "getchar()". I attempt to handle this on lines 16 and 17, but it appears to not work as expected. So i would like to know logic behind @mch answer , why did he decide to break after first getchar. – Pedja Aleksic Sep 23 '14 at 16:21
  • The answer your TA should give you: Look at your loop -- the very first thing you do is read two characters. What happens if the first character is a newline? Do you even want to read a second character in this case? What do you need to do to avoid reading a second character when the first is a newline? – Chris Dodd Sep 23 '14 at 20:25

1 Answers1

0

I typed PAR, hit one time enter and got APR. I believe this is what you want.

Is there some way to kill \n for getchar?

You need to do something like this:

char1 = getchar();
getchar(); // To kill `\n`
char2 = getchar();
getchar(); // To kill `\n`

Source.

Also as suggested by mch, you can do:

if(pch == '\n') break; after pch=getchar();

So you should change your program to this:

#include <stdio.h>
int main(void) {
  int pch;  //first
  int ch;  //second

  while (1) {
    pch = getchar();
    if (pch == '\n') // when you hit enter once, break the loop
          break;
    ch = getchar();

    if (((pch >= 'A' && pch <= 'Z')) && ((ch >= 'A' && ch <= 'Z'))) {
      putchar(ch);
      putchar(pch);
    }

    if ((pch >= 'A' && pch <= 'Z') && ch == '\n')
      putchar(pch);
    if (pch == '\n' || ch == '\n')
      break;
  }
  return 0;
}
Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • on odd chars (3,5,7...) it works but are you sure it works on even. When i typed PARA i hit enter twice . – Pedja Aleksic Sep 23 '14 at 16:10
  • I thought you meant the other way, sorry. I think my updated answer is what you want. Credits go to mch. I think this a good question for a first one, so I am going to upvote you @PedjaAleksic. – gsamaras Sep 23 '14 at 16:13