简体   繁体   中英

how to make scanf not ignore spaces and \n in C

so, i am currently studying C code in class, we got an assignemnt for ceaser encoder. I managed to do it, however i now hit an issue regarding the fact they want it to acknowledge spaces and new lines in it. i already checked online and saw more than a few options but there are a few issues:

1) I have to use scanf so do not give a second option

2) I am working with regular chars and not with strings, because in this course they yet to have taught it, so do not offer using char[] or strings or char* because i am not supposed to use them. thanks in advance.

You can read a single character at a time with scanf , and the whitespace will not be ignored:

#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("Will print all characters typed, use ctrl-D to quit\n");
  char c;
  while (scanf("%c", &c) != EOF) {
    printf("%c", c);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM