putchar ends only on pressing enter (C)

45 Views Asked by At

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!

0

There are 0 best solutions below