c - Reading whole input instead of single line -
this question has answer here:
- how use eof run through text file in c? 4 answers
 
i have written program changes lowercase letter uppercase. problem is, dont know how make read whole text instead of 1 line. program returns output after pressing enter , want so, after ctrl+z.
#include <stdlib.h> #include <stdio.h>   void makeupper(char *s) {     int i;     for(i = 0; s[i] != '\0'; i++){         s[i] = toupper(s[i]);     }     printf("%s", s); }   int main() {     char string[1000];      fgets(string, 1000, stdin);      makeupper(string);     return 0; }      
fgets() stop once encounters newline. so, can't workaround read multiple lines. so, you'll have @ alternatives.
one way is use getchar() loop , read long there's room in buffer or eof received.:
int main(void) {      char string[1000];     size_t = 0;      {         int ch = getchar();         if (ch == eof) break;         string[i] = ch;         i++;     } while (i < sizeof string - 1);     string[i] = 0;      makeupper(string);     return 0; }   remember, ctrl+z works on windows (to send eof). on *nix-like systems, you'll have use ctrl+d send eof.
Comments
Post a Comment