在c中,如果要遍历一个数组,可以使用for
char str[] = 'abcd';
for(int i = 0; i < strlen(str); i++)
{
printf("%d %c\r\n", i, str[i]);
}
0 a
1 b
2 c
3 d
python 中可以使用:
str = 'abcd'
for index in range(len(str)):
print index, str[index]
0 a
1 b
2 c
3 d
还可以使用enumerate:
str = 'abcd'
for index, c in enumerate(str):
print index, c
0 a
1 b
2 c
3 d