我正在创建一个非常基本的 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/