0

So this simple program is suppose to change each occurrence of 's' or "ss" to "th".

Here is my code:

#include <stdio.h>
#include <ctype.h>    

.
.
.

printf("Enter a sentence:\t");

int ch;

while ((ch = tolower(getchar())) != '.'){

    if(ch == 's'){
        int nxtCh = tolower(getchar());
        if(nxtCh == 's'){
            putchar('t');
            putchar('h');
        }else{
            putchar('t');
            putchar('h');
            putchar(nxtCh);
        }
    }else{
        putchar(ch);
    }
}
putchar('.');

Once a period is entered, the program is still waiting for me to press enter for everything to actually register. What I want is for the program to end the while loop immediately once a period is entered.

Am I doing something wrong? How can I achieve this?

I am working through Xcode on Mac

Thank you!

  • Nope, you aren't doing anything wrong. That's just the way the terminal works. In order for the terminal to allow editing (e.g. backspace and then type something else) it has to buffer the line until you press enter. The methods for turning off line buffering vary by operating system, and are not part of the C standard. – user3386109 Sep 27 '18 at 18:47
  • https://stackoverflow.com/questions/1798511/how-to-avoid-pressing-enter-with-getchar – BurnsBA Sep 27 '18 at 18:48

0 Answers0