用指针打印字符串长度(指针 & fgets函数)

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #define MAX 1024
 5 
 6 int main(){
 7  //用指针打印字符串长度
 8  char str[MAX];
 9  char *target = str;
10  int length = 0;
11 
12  printf("请输入一个字符串:");
13  fgets(str, MAX, stdin);
14 
15 //fgets(字符数组,数组长度,stdin), stdin是标准输入
16 
17  while (*(target++) != '\0'){
18   length++;
19  }
20  printf("您输入了%d个字符\n", length - 1);
21  //由于fgets函数会读取到回车,因此length - 1
22  
23  system("pause");
24  return 0;
25 }

 

posted @ 2020-04-05 19:14  听说在北郭  阅读(459)  评论(0)    收藏  举报