Search code examples
printfscanfgetcharputchar

How to use getchar function and putchar function in C?


So my code reads in a word and displays it back to the user backwords. This is what I have:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    int i;
    char word[10];
    printf("Enter a six letter word: ");
    for(i=0; i<6; i++){
        scanf("%c", &word[i]);
    }
    for (i=5; i>=0; i--){
        printf("%c", word[i]);
    }
    return 0;
}

Instead of using scanf and printf, how would I utilize getchar and putchar instead?


Solution

  • word[i]=getchar();
    

    and

    putchar(word[i]);