用c语言判断字符串为空,如何检查C字符串是否为空

如何检查C字符串是否为空

我正在用C写一个非常小的程序,需要检查某个字符串是否为空。 为了这个问题,我简化了代码:

#include

#include

int main() {

char url[63] = {'\0'};

do {

printf("Enter a URL: ");

scanf("%s", url);

printf("%s", url);

} while (/*what should I put in here?*/);

return(0);

}

我希望该程序停止循环,如果用户只是按Enter键而不输入任何内容。

codedude asked 2020-06-24T16:10:33Z

11个解决方案

55 votes

由于C样式字符串始终以空字符(strcmp)终止,因此您可以通过以下方式检查字符串是否为空:

do {

...

} while (url[0] != '\0');

或者,您可以使用strcmp函数,该函数虽然过大,但可能更易于阅读:

do {

...

} while (strcmp(url, ""));

请注意,如果字符串不同,则strcmp返回一个非零值,如果字符串相同,则返回0,因此此循环继续循环直到字符串为非空。

希望这可以帮助!

templatetypedef answered 2020-06-24T16:10:55Z

15 votes

如果要检查字符串是否为空:

if (str[0] == '\0')

{

// your code here

}

nabroyan answered 2020-06-24T16:11:15Z

7 votes

如果第一个字符恰巧是'\0',则您有一个空字符串。

这是您应该做的:

do {

/*

* Resetting first character before getting input.

*/

url[0] = '\0';

// code

} while (url[0] != '\0');

Mohamad Ali Baydoun answered 2020-06-24T16:11:40Z

4 votes

通常来说,考虑到-1会忽略空格(空格,制表符,换行符),您将很难在这里得到一个空字符串...但是无论如何,-1实际上会返回成功匹配的次数...

从手册页:

成功匹配和分配的输入项的数量,该数量可以少于提供的数量,如果早期匹配失败,则为零。

因此,如果他们设法通过空字符串(例如-1)来解决问题,则只需检查返回结果即可。

int count = 0;

do {

...

count = scanf("%62s", url); // You should check return values and limit the

// input length

...

} while (count <= 0)

请注意,您必须进行的检查不多,因为在我给出的示例中,您会得到-1的信息,该信息再次在手册页中进行了详细介绍:

如果在第一次成功转换或匹配失败发生之前达到输入结束,则返回EOF值。 如果发生读取错误,也会返回EOF,在这种情况下,将设置流的错误指示符(请参见ferror(3)),并且设置errno表示错误。

Mike answered 2020-06-24T16:12:23Z

3 votes

您可以检查scanf的返回值。此代码将一直位于该位置,直到接收到字符串为止。

int a;

do {

// other code

a = scanf("%s", url);

} while (a <= 0);

squiguy answered 2020-06-24T16:12:43Z

2 votes

strlen(url)

返回字符串的长度。 它计算所有字符,直到找到一个空字节。 您的情况下,请对照0进行检查。

或仅通过以下方式手动检查:

*url == '\0'

flyingOwl answered 2020-06-24T16:13:11Z

2 votes

您可以尝试这样:-

if (string[0] == '\0') {

}

在您的情况下,它可能像:

do {

...

} while (url[0] != '\0')

;

Rahul Tripathi answered 2020-06-24T16:13:40Z

2 votes

首先用fgets()替换scanf() ...

do {

if (!fgets(url, sizeof url, stdin)) /* error */;

/* ... */

} while (*url != '\n');

pmg answered 2020-06-24T16:14:00Z

1 votes

最简单的方法是:

do {

// Something

} while (*url);

基本上,*url将在数组的第一个位置返回char; 由于C字符串以空值结尾,因此如果字符串为空,则其第一个位置为字符'\0',其ASCII值为0; 由于C逻辑语句将每个零值都视为false,因此当字符串的第一个位置为非空(即字符串不为空)时,此循环将继续进行。

推荐阅读,如果您想更好地理解这一点:

C字串:[https://www.tutorialspoint.com/cprogramming/c_strings.htm]

C阵列:[https://www.tutorialspoint.com/cprogramming/c_arrays.htm]

数组和指针之间的关系:[https://www.programiz.com/c-programming/c-pointers-arrays]

逻辑运算符:[https://www.tutorialspoint.com/cprogramming/c_logical_operators.htm]

Haroldo_OK answered 2020-06-24T16:14:46Z

0 votes

我写下了这个宏

#define IS_EMPTY_STR(X) ( (1 / (sizeof(X[0]) == 1))/*type check*/ && !(X[0])/*content check*/)

所以会是

while (! IS_EMPTY_STR(url));

此宏的好处在于它是类型安全的。 如果将其他内容放入char指针之外,则会出现编译错误。

user1537765 answered 2020-06-24T16:15:15Z

-1 votes

使用strtok(),只需一行即可完成:“ if(strtok(s,“ \ t”)== NULL)“。例如:

#include

#include

int is_whitespace(char *s) {

if (strtok(s," \t")==NULL) {

return 1;

} else {

return 0;

}

}

void demo(void) {

char s1[128];

char s2[128];

strcpy(s1," abc \t ");

strcpy(s2," \t ");

printf("s1 = \"%s\"\n", s1);

printf("s2 = \"%s\"\n", s2);

printf("is_whitespace(s1)=%d\n",is_whitespace(s1));

printf("is_whitespace(s2)=%d\n",is_whitespace(s2));

}

int main() {

char url[63] = {'\0'};

do {

printf("Enter a URL: ");

scanf("%s", url);

printf("url='%s'\n", url);

} while (is_whitespace(url));

return 0;

}

nathanielng answered 2020-06-24T16:15:35Z

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值