鱼C论坛

 找回密码
 立即注册
查看: 1696|回复: 1

[已解决]c语言统计输入的中英文长度,为啥我的回车啥也没有

[复制链接]
发表于 2020-9-22 19:20:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <string.h>
  3. //************************统计输入的中英文的总个数*************************
  4. int main()
  5. {
  6.         char str[100];
  7.         int i = 0,count = -1;   //count赋值-1,因为在读取字符串时候发现,字符串结束时的最后一位并不是'\0',下一位才是
  8.         printf("enter a string:");
  9.         gets(str) ;
  10.         printf("*str = %s , *(str + 1) = %s , *(str + 2 ) = %c \n",*str,*(str + 1),*(str + 2));
  11.        
  12.         for(i = 0 ;  ; i++)
  13.         {
  14.                 if (*(str + i )== '\0')
  15.                 {
  16.                         break;
  17.                 }
  18.                 else
  19.                 {
  20.                         count += 1;
  21.                 }
  22.         }
  23.         printf("the lenth of the string is: %d",count);
  24. }
复制代码
最佳答案
2020-9-22 19:24:16
  1. #include <stdio.h>
  2. #include <string.h>
  3. //************************统计输入的中英文的总个数*************************
  4. int main()
  5. {
  6.     char str[100];
  7.     int i = 0, count = 0;   //count赋值-1,因为在读取字符串时候发现,字符串结束时的最后一位并不是'\0',下一位才是
  8.     printf("enter a string:");
  9.     gets(str);
  10.     printf("*str = %c , *(str + 1) = %c , *(str + 2 ) = %c \n", *str, *(str + 1), *(str + 2));

  11.     for (i = 0; ; i++)
  12.     {
  13.         if (*(str + i) == '\0')
  14.         {
  15.             break;
  16.         }
  17.         else
  18.         {
  19.             count += 1;
  20.         }
  21.     }
  22.     printf("the lenth of the string is: %d", count);
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-22 19:24:16 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <string.h>
  3. //************************统计输入的中英文的总个数*************************
  4. int main()
  5. {
  6.     char str[100];
  7.     int i = 0, count = 0;   //count赋值-1,因为在读取字符串时候发现,字符串结束时的最后一位并不是'\0',下一位才是
  8.     printf("enter a string:");
  9.     gets(str);
  10.     printf("*str = %c , *(str + 1) = %c , *(str + 2 ) = %c \n", *str, *(str + 1), *(str + 2));

  11.     for (i = 0; ; i++)
  12.     {
  13.         if (*(str + i) == '\0')
  14.         {
  15.             break;
  16.         }
  17.         else
  18.         {
  19.             count += 1;
  20.         }
  21.     }
  22.     printf("the lenth of the string is: %d", count);
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 17:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表