strlen()计算数组的问题
本题要求:把一个单词读入字符数组,然后反向打印该单词。我的代码如下:(题中功能已实现,但请大家注意最后一个
printf语句strlen()的使用)
#include<stdio.h>
#include<string.h>
int main()
{
int index = 0,len;
char str[100];
char ch;
printf("Input the words: ");
while ((ch = getchar()) != '\n')
str[index++] = ch;
printf("\nTurn round the words: ");
for (len = index - 1;len >= 0;len--)
printf("%c",str[len]);
printf("\n");
printf("\nThe length is %d ,Why ?\n",strlen(str)); //此处我欲打印数组中单词的长度。问题有二:
// 1)如何用strlen打印单词长度,2 )strlen(len)打印的是什么长度,为什么它的值总是121 ?
return 0;
}