为什么它在第二个字符串中打印空字符?
声明字符数组应该在结尾处自动添加空字符.这取决于编译器吗?
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool ChckStrng(char *str);
void main()
{
char a[] = "hello";
char b[] = "abc";
printf("a:%d\n", ChckStrng(a));
printf("b:%d\n", ChckStrng(b));
}
bool ChckStrng(char *str)
{
int count[26];
while(str != NULL)
{
printf(":%d:\n", *str - 'a');
if(++count[*str - 'a'] > 1)
return false;
str = str + 1;
}
printf("end\n");
return true;
}
Run Code Online (Sandbox Code Playgroud)
输出1:
:7 :: 4 :: 11:11:a:0
:0 :: 1 :: 2:: - 97 :: -97:b:0