鱼C论坛

 找回密码
 立即注册
查看: 1066|回复: 9

[已解决]求助一段代码。输入一句话,输出的结果为那句话中有几个字母。

[复制链接]
发表于 2020-6-29 09:00:58 | 显示全部楼层 |阅读模式
5鱼币
就比如说我输入;“Hello, world.”
然后输出的结果为:“这句话里有 10 个字母。”
如果输入的句子里比较长,并且包含有逗号,句号,问号,感叹号的话,这些符号都不能计算在内,只考虑字母的数量。
不能使用isalpha。
非常感谢。
最佳答案
2020-6-29 09:00:59
  1. #include <stdio.h>
  2. #include <string.h>

  3. int  main()
  4. {
  5.     char a[100];
  6.     printf("请输入一段英文:");
  7.     gets(a);
  8.     int i = 0;
  9.     int count=0;
  10.    
  11.     for (i = 0; i < strlen(a); i++)
  12.     {
  13.         if ( (a[i]>=65 && a[i]<= 90) || (a[i]>=97 && a[i] <= 122) )
  14.         count +=1;

  15.     }
  16.     int b = strlen(a);
  17.     printf("字符串长度:%d\n",b);
  18.     printf("一共有%d个字母",count);
  19.     return 0;
  20. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-29 09:00:59 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <string.h>

  3. int  main()
  4. {
  5.     char a[100];
  6.     printf("请输入一段英文:");
  7.     gets(a);
  8.     int i = 0;
  9.     int count=0;
  10.    
  11.     for (i = 0; i < strlen(a); i++)
  12.     {
  13.         if ( (a[i]>=65 && a[i]<= 90) || (a[i]>=97 && a[i] <= 122) )
  14.         count +=1;

  15.     }
  16.     int b = strlen(a);
  17.     printf("字符串长度:%d\n",b);
  18.     printf("一共有%d个字母",count);
  19.     return 0;
  20. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-29 09:10:46 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.         char a[40],result = 0, i = 0;
  6.         scanf("%s",a);
  7.         for(;i<strlen(a);i++)
  8.         {
  9.                 if((a[i] >= 65&&a[i] <=90)||(a[i] >= 97 && a[i] <= 122))
  10.                 {
  11.                         result += 1;
  12.                 }
  13.         }
  14.         printf("字母有%d个",result);
  15.         return 0;
  16. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-29 09:11:40 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.         char a[40];
  6.         int i = 0,result = 0;
  7.         scanf("%s",a);
  8.         for(;i<strlen(a);i++)
  9.         {
  10.                 if((a[i] >= 65&&a[i] <=90)||(a[i] >= 97 && a[i] <= 122))
  11.                 {
  12.                         result += 1;
  13.                 }
  14.         }
  15.         printf("字母有%d个",result);
  16.         return 0;
  17. }
复制代码

又改了一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-29 17:07:10 | 显示全部楼层
  1. #include<stdio.h>


  2. int main() {
  3.     unsigned long long result = 0;

  4.     for (char temp = getchar(); temp && temp != '\n' && temp != '\r'; temp = getchar()) {
  5.         if ((temp >= 'a' && temp <= 'z') || (temp >= 'A' && temp <= 'Z')) {
  6.             ++result;
  7.         }
  8.     }

  9.     printf("这句话里有 %llu 个字母。", result);
  10.     return 0;
  11. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-29 17:07:20 | 显示全部楼层

效率不行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-29 21:25:32 | 显示全部楼层
  1. #include<iostream>
  2. #include<string>
  3. int main()
  4. {
  5.         std::string str;
  6.         std::cin >> str;
  7.         unsigned long long count = 0;
  8.         for (int i = 0;str[i]!='\0'; i++)
  9.                 if (str[i] <= 'z' && str[i] >= 'a' || str[i] <= 'Z' && str[i] >= 'A') count++;
  10.         std::cout << "这句话里有" << count << "个字母。" << std::endl;
  11.         return 0;
  12. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-29 21:53:41 | 显示全部楼层

中间有空格的搞不定
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-30 18:24:24 | 显示全部楼层
  1. import re

  2. string = str(input('请输入需要匹配的内容:'))
  3. p = re.compile('[a-zA-Z]')
  4. m = p.findall(string)

  5. print('这句话里面有 %s 个字母。' % len(m))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-7-1 04:54:16 | 显示全部楼层

我试着去运行你写的这段代码,但是出了这个问题,请问怎么解决呢?
截屏2020-06-30 16.53.01.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-16 14:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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