检查字符串是否为空,然后继续

标签 c shell scanf strcmp

我正在创建一个非常基本的 shell 。当用户按下 Enter 键而不输入任何命令时,程序应该转到下一行并继续。

如果用户键入任何命令,程序应打印一条文本消息。

我遇到空字符串条件问题。

尝试使用另一个字符数组strcmp()

#include <stdio.h>
#include <string.h>

int main(void){
    char input[256];
    char str[4] = {"exit"};

    do
    {
        printf("CSC327> ");
        scanf("%[^\n]", &input);
        if(getchar() == '\n')
            continue;       
        if(strcmpi(input, str))
            printf("Command not found!\n");
    }while(strcmpi(input,str));

    printf("\nGoodbye.");

    return 0;
}

最佳答案

如果你想检查字符串是否为空,只需使用

int lenght = strlen(your_string)

返回字符串的长度。如果为0,则字符串为空。

关于检查字符串是否为空,然后继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58403454/

相关文章:

macos - 如何使用终端批量重命名文件?

c - 为什么我的程序在第一个 for 循环之后结束?

c - 在 Linux shell 中实现流水线

linux - 使用 find -exec 重定向标准输出

javascript - 如何在 webkit2gtk 中将 native 代码公开给 javascript

linux - While Read 存储每个没有空格的用户条目

c - 使用scanf读取字符

c - 循环重复 scanf 和 printf

c - 在 C 中使用带参数的 system()

c - C 代码的 R 内联编译在 Windows 上失败