a note for use of strlen

View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 int distance(char str1[])
 4 {
 5  int len;
 6  len=strlen(str1);
 7   return len;
 8 }
 9 int main()
10 {
11  char str[2][10]; // if str[2][7]
12  int i;
13  for(i=0;i<2;i++)
14  scanf("%s", str[i]);
15  int len1, len2;
16  len1 = distance(str[0]);
17  len2 = distance(str[1]);
18  printf("%d, %d\n", len1, len2);
19  return 0;
20 }
intput:
abcdefg
abcdefg
output:
7, 7
 
//if str[2][7]
input:
abcdefg
abcdefg
output:
14, 7
 
comment: there is a need for one position for '\0' in str[2][7] since strlen count number stopping by '\0'!
posted @ 2013-01-16 02:15  NiJc  阅读(98)  评论(0编辑  收藏  举报