查找C字符串长度(不使用的strlen) [英] Find length of string in C (without using strlen)
本文介绍了查找C字符串长度(不使用的strlen)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下code不打印任何东西 -
INT的main(){ 字符*输入=这是一个字符串;
find_length(输入);
返回0;}
无效find_length(字符*输入){ 的printf(%S,输入);
INT长度= 0;
而(!输入[长度] ='\\ 0');
{
长++;
的printf(%i的,长度);
}}
解决方案
你有你身后环一个额外的分号:
而(!输入[长度] ='\\ 0');
^不属于这里!
所以它是停留在一个无限循环。摆脱它。
此外,有可能是流缓冲。尝试添加一些 \\ n
。或致电 fflush(标准输出)
来刷新输出流。
无效find_length(字符*输入){ 的printf(%S \\ n,输入);
INT长度= 0;
而(输入[长度] ='\\ 0'!)//删除;
{
长++;
的printf(%I \\ N,长度);
}}
The code below does not print anything -
int main() {
char *input = "This is a string";
find_length(input);
return 0;
}
void find_length(char *input){
printf("%s", input);
int length = 0;
while(input[length]!='\0');
{
length++;
printf("%i", length);
}
}
解决方案
You have an extra semicolon behind your loop:
while(input[length]!='\0');
^ does not belong here!!!
So it is stuck in an infinite loop. Get rid of it.
Furthermore, there could be stream buffering. Try adding some \n
. Or call fflush(stdout)
to flush the output stream.
void find_length(char *input){
printf("%s\n", input);
int length = 0;
while(input[length]!='\0') // remove ;
{
length++;
printf("%i\n", length);
}
}
这篇关于查找C字符串长度(不使用的strlen)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文