I am trying to accept a string from the standard input in Linux, take the given string and change the ‘A'(s) and ‘a'(s) to ‘@’, and output the altered string.
In linux I am running this: echo “This problem is an EASY one” | ./a2at
My a2at.c program contains this:
#include <stdio.h> #include <string.h> int main(int argc, char *words[]) { int i = 0; char b[256]; while(words[i] != NULL) { b[i] = *words[i]; if(b[i] =='a' || b[i]=='A') { b[i] = '@'; } printf("%c",b[i]); } return 0; }
Any help would be really appreciated! I know that I am pretty far off from the right code.
Advertisement
Answer
As @BLUEPIXY said in his comment, you could use getchar function from stdio.h, just do man getchar in your shell to have more details about the usages. This code could help you, but don’t hesitate to use the man command 🙂 !
#include <stdio.h> int main(void) { int c; while ((c=getchar()) && c!=EOF) { if (c == 'a' || c== 'A') c = '@'; write(1, &c, 1); // Or printf("%c", c); } return (0); }