如何从Linux命令行接受C中的标准输入

时间:2016-02-14 22:30:52

标签: c linux command-line

我试图从Linux中的标准输入接受一个字符串,取出给定的字符串并将'A'(s)和'a'(s)更改为'@',并输出更改后的字符串。

在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;

}

任何帮助都会非常感激!我知道我离正确的代码还很远。

2 个答案:

答案 0 :(得分:1)

您可以使用getchar()一次阅读一个字符,或fgets()以便每次都读取完整的一行。

最简单的解决方案是在简单的无限循环中使用getch

while (1) {
    int ch = getchar();
    if (ch == EOF) {
        break;
    } else if (ch == 'a' || ch == 'A') {
        putchar('@');
    } else {
        putchar(ch);
    }
}

答案 1 :(得分:0)

正如@BLUEPIXY在他的评论中所说,你可以使用stdio.h中的getchar函数,只需在shell中使用man getchar来获取有关用法的更多细节。这段代码可以帮助你,但不要犹豫使用man命令:)!

<div v-for="slot in card" class="panel panel-default">
<div class="panel-heading">{{ slot.name }}</div>
<div class="panel-body">
    <p v-for="wrestler in slot.wrestlers">
        {{ wrestler.team }}
        <p v-if="true">okay</p>
        {{ wrestler.team }}
    </p>
</div>
相关问题