我想找到是否有任何方法可以找到任何字符串的长度C.
这是我的方式:
#include <stdio.h>
int main()
{
char s[10] = "hello";
int i , len = 0;
for(i = 0; s[i] != '\0'; i++)
{
len++
}
printf("length of string is: %d" , len);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想找到,如果有任何方法可以在一行代码中获得字符串的长度.
我对下面的代码很困惑.
def a(x):
print(x)
if x > 0:
a(x - 1)
print(x) #I am confused with this print statement
a(5)
Run Code Online (Sandbox Code Playgroud)
以上代码输出:
5
4
3
2
1
0
0
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
直到0我理解它是如何打印的,然后是为什么它按升序打印.
可变x正在改变所以我想输出将是x的最后分配的值是0.
我的预测输出:
5
4
3
2
1
0
0
0
0
0
0
0
Run Code Online (Sandbox Code Playgroud)
那么它如何跟踪 x ...的值?
有人可以简要解释递归函数中实际发生的事情以及变量如何存储在其中.