[英]How to Accept Standard Input in C from the Linux Command Line
我正在尝试从Linux中的标准输入接受一个字符串,采用给定的字符串并将'A'和'a'更改为'@',然后输出更改后的字符串。
在linux中,我正在运行此命令:echo“此问题很容易解决” ./a2at
我的a2at.c程序包含以下内容:
#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;
}
任何帮助将非常感激! 我知道我离正确的代码还差得很远。
正如@BLUEPIXY在他的评论中所说,您可以使用stdio.h中的getchar函数,只需在shell中使用man getchar以获得有关用法的更多详细信息。 这段代码可以为您提供帮助,但是请不要犹豫使用man命令:)!
#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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.