检查字符串中是否存在“\0” [英] Checking whether '\0' exists in the string

查看:135
本文介绍了检查字符串中是否存在“\0”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们应该如何检查字符串中是否存在''\ 0''字符,因为我感到困惑,因为有些书提到了我们必须检查的
我们是否需要确保将

''null-terminated-string''传递给某些函数,

,例如''atoi''。 (我们假设字符串是使用数组提取的,而不是

字符串文字)


此程序为所有人显示''\0''的空白3例。为什么?而且我试图通过使用数组传递一些字符串来
传递一些字符串,而没有

附加''\0''并且它有效。这里有什么不一致吗?


char * str =" Hello" ;; //案例1

char str [] = {''H'',''e'',''l'','l'',''o''}; // Case2

char str [] = {''H'',''e'',''l'','l'',''o''}; // Case3

int length = strlen(str);

char * p;

int i;


//测试字符串中是否存在'0''(b = str,i = 0; i< = length; p ++,i ++){

printf(" Character%d is:%c\ n,i,* p);

}


任何人都可以澄清这个问题?谢谢

解决方案

Kayle写道:

我们应该如何检查''\ 0''字符是否存在于字符串,因为我很困惑,有些书提到我们必须检查是否需要确保我们将
''null-terminated-string''传递给某些函数,<比如''atoi''。 (我们假设字符串是使用数组提取的,而不是字符串文字)

对于所有3种情况,此程序显示'\ 0的空白。为什么?我试图传递一些使用数组形成的字符串,而不是附加''\0''并且它有效。这里有什么不一致吗?

char * str =" Hello" ;; // Case1
char str [] = {''H'',''e'',''l'','l'',''o''}; // Case2
char str [] = {''H'','e'',''l'','l'',''o''}; // Case3
int length = strlen(str);
char * p;
int i;

//测试是否存在0 string
for(p = str,i = 0; i< = length; p ++,i ++){
printf(" Character%d is:%c\ n,i,* p );
}

任何人都可以澄清这个问题?谢谢




在你的例子中,你用'strlen'来获取长度 -

返回'char'的数量达到*但不包括*

终结符(''\ 0'')。


Case1是一个指向字母H,e,l,l,o,\\0的指针。


在Case2和Case3中,你得到(非)幸运。

作为练习,打印出strlen调用后的'length'。如果

恰好是5,那它纯属偶然。


HTH,

--ag


-

Artie Gold - 德克萨斯州奥斯汀


Artie Gold写道:< blockquote class =post_quotes> Kayle写道:

我们应该如何检查字符串中是否存在''\0''字符,因为我感到困惑书中提到
我们必须检查是否需要确保我们将
''null-terminated-string''传递给某些函数,例如''atoi''。 (我们假设字符串是使用
数组提取的,而不是字符串文字)

对于所有3个案例,此程序显示'\ 0的空白。为什么?而且我试图传递一些使用数组形成的字符串,而不是附加''\0''并且它有效。这里有什么不一致吗?

char * str =" Hello" ;; // Case1
char str [] = {''H'',''e'',''l'','l'',''o''}; // Case2
char str [] = {''H'','e'',''l'','l'',''o''}; // Case3
int length = strlen(str);
char * p;
int i;

//测试是否存在0 string
for(p = str,i = 0; i< = length; p ++,i ++){
printf(" Character%d is:%c\ n,i,* p );
}

任何人都可以澄清这个问题?谢谢

在你的例子中,你用'strlen'来获取长度 - 返回
'char'的数量达到*但不包括*终结符(''\ 0'')。

Case1是指向字符H,e,l,l的指针, ''o'',''\'''。



"是指向包含chars的内存的char的指针......
BTW - 有区别吗?

作为练习,打印出strlen调用后的'length'。如果它恰好是5,那它纯属偶然。

HTH,
- ge



--ag

-

Artie Gold - 德克萨斯州奥斯汀




Kayle < KA *** @ yahoo.com>在消息中写道

新闻:LU ******************* @ news-server.bigpond.net.au ...

我们应该如何检查字符串中是否存在''\0''字符,因为我很困惑,有些书提到
我们必须检查是否需要确保我们将
''null-terminated-string''传递给某些函数,例如''atoi''。 (我们假设字符串是使用数组提取的,
不是字符串文字)


书籍告诉你要确保你在程序中使用的字符串

以空值终止。这并不意味着在调用诸如atoi()之类的函数之前,必须编写代码来检查空终止字符的



只要确保所有字符串都是null终止。

对于所有3个案例,此程序显示'\ 0的空白。为什么?我正在尝试
来传递一些使用数组形成的字符串,而不是附加''\0''并且它有效。这里有什么不一致吗?

char * str =" Hello" ;; // Case1
char str [] = {''H'',''e'',''l'','l'',''o''}; // Case2
char str [] = {''H'','e'',''l'','l'',''o''}; // Case3
int length = strlen(str);
char * p;
int i;


案例1已经以空值终止。案例2和3(看起来相同,顺便说一句),

并非必须如此,因此您必须在数组之前附加''\ 0''

在任何需要以null结尾的字符串的代码中使用它们。

//测试字符串中是否存在0,(p = str,i = 0; i< = length; p ++,i ++){
printf(" Character%d is:%c\ n,i,* p);
}

任何人都可以澄清这个问题?谢谢


你不觉得使用strlen()很有意思,这样你就可以编写一个循环

来测试''\0' '当strlen()本身需要以空字符结尾的字符串时?你很幸运,strlen()和循环在阵列中的某个地方遇到了''\0''。如果您重新运行

程序,则无法保证相同的行为。



How should we check if the ''\0'' characters exists in the string as I am
confused that some books mentioned
that we have to check whether we need to make sure that we pass the
''null-terminated-string'' to some functions,
such as ''atoi''. (We assume that the string was extracted using an array, not
string literal)

This program shows blank for ''\0'' for all 3 cases. Why ? And I was trying to
pass some strings formed using an array, without
appending ''\0'' and it worked. Was there any inconsistencies here?

char *str = "Hello"; // Case1
char str[] = {''H'', ''e'', ''l'', ''l'', ''o''}; // Case2
char str[] = {''H'', ''e'', ''l'', ''l'', ''o''}; // Case3
int length = strlen(str);
char *p;
int i;

// Testing whether ''0'' exists in the string
for (p = str, i = 0; i <= length; p++, i++) {
printf("Character %d is: %c\n",i,*p);
}

Anybody can clarify this issue? Thanks

解决方案

Kayle wrote:

How should we check if the ''\0'' characters exists in the string as I am
confused that some books mentioned
that we have to check whether we need to make sure that we pass the
''null-terminated-string'' to some functions,
such as ''atoi''. (We assume that the string was extracted using an array, not
string literal)

This program shows blank for ''\0'' for all 3 cases. Why ? And I was trying to
pass some strings formed using an array, without
appending ''\0'' and it worked. Was there any inconsistencies here?

char *str = "Hello"; // Case1
char str[] = {''H'', ''e'', ''l'', ''l'', ''o''}; // Case2
char str[] = {''H'', ''e'', ''l'', ''l'', ''o''}; // Case3
int length = strlen(str);
char *p;
int i;

// Testing whether ''0'' exists in the string
for (p = str, i = 0; i <= length; p++, i++) {
printf("Character %d is: %c\n",i,*p);
}

Anybody can clarify this issue? Thanks



In your example, you''ve used `strlen'' to get the length -- which
returns the number of `char''s up to *but not including* the
terminator (''\0'').

Case1 is a pointer to the chars ''H'', ''e'', ''l'', ''l'', ''o'', ''\0''.

In Case2 and Case3, you just got (un)lucky.
As an exercise, print out what `length'' is after the strlen call. If
it happens to be 5, it''s purely happenstance.

HTH,
--ag

--
Artie Gold -- Austin, Texas


Artie Gold wrote:

Kayle wrote:

How should we check if the ''\0'' characters exists in the string as I am
confused that some books mentioned
that we have to check whether we need to make sure that we pass the
''null-terminated-string'' to some functions,
such as ''atoi''. (We assume that the string was extracted using an
array, not
string literal)

This program shows blank for ''\0'' for all 3 cases. Why ? And I was
trying to
pass some strings formed using an array, without
appending ''\0'' and it worked. Was there any inconsistencies here?

char *str = "Hello"; // Case1
char str[] = {''H'', ''e'', ''l'', ''l'', ''o''}; // Case2
char str[] = {''H'', ''e'', ''l'', ''l'', ''o''}; // Case3
int length = strlen(str);
char *p;
int i;

// Testing whether ''0'' exists in the string
for (p = str, i = 0; i <= length; p++, i++) {
printf("Character %d is: %c\n",i,*p);
}

Anybody can clarify this issue? Thanks
In your example, you''ve used `strlen'' to get the length -- which returns
the number of `char''s up to *but not including* the terminator (''\0'').

Case1 is a pointer to the chars ''H'', ''e'', ''l'', ''l'', ''o'', ''\0''.


"is a pointer to char pointing to memory containing the chars..."

In Case2 and Case3, you just got (un)lucky. BTW - is there a difference?
As an exercise, print out what `length'' is after the strlen call. If it
happens to be 5, it''s purely happenstance.

HTH,
--ag


--ag
--
Artie Gold -- Austin, Texas



"Kayle" <ka***@yahoo.com> wrote in message
news:LU*******************@news-server.bigpond.net.au...

How should we check if the ''\0'' characters exists in the string as I am
confused that some books mentioned
that we have to check whether we need to make sure that we pass the
''null-terminated-string'' to some functions,
such as ''atoi''. (We assume that the string was extracted using an array, not string literal)

The books are telling you to make sure that the strings you use in a program
are null-terminated. This does not mean that you have to write code to check
for the null terminating character before calling a function such as atoi().
Just make sure that all strings are null-terminated.
This program shows blank for ''\0'' for all 3 cases. Why ? And I was trying to pass some strings formed using an array, without
appending ''\0'' and it worked. Was there any inconsistencies here?

char *str = "Hello"; // Case1
char str[] = {''H'', ''e'', ''l'', ''l'', ''o''}; // Case2
char str[] = {''H'', ''e'', ''l'', ''l'', ''o''}; // Case3
int length = strlen(str);
char *p;
int i;
Case 1 is already null-terminated. Cases 2 and 3 (which look the same, BTW),
are not necessarely so and therefore you must append ''\0'' to the arrays
prior to using them in any code that expects null-terminated strings.

// Testing whether ''0'' exists in the string
for (p = str, i = 0; i <= length; p++, i++) {
printf("Character %d is: %c\n",i,*p);
}

Anybody can clarify this issue? Thanks

Don''t you find it interesting to use strlen() so that you can write a loop
to test for ''\0'' when strlen() itself expects a null-terminated string? You
just got lucky that both strlen() and the loop encountered ''\0'' somewhere in
the array. There''s no guarantee of the same behaviour if you re-run the
program.



这篇关于检查字符串中是否存在“\0”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆