鱼C论坛

 找回密码
 立即注册
查看: 890|回复: 3

[已解决]字符串和指针

[复制链接]
发表于 2021-8-10 13:33:08 | 显示全部楼层 |阅读模式

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

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

x
/****题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。****/
#include "stdio.h"
int  main()
{
        char str,*p;
        int letters=0,space=0,digit=0,others=0;
        printf("Please input character:\n");
        scanf("%s",&str);
        p=&str;
        while ((*p++)!='\0')/****原代码没有用指针,只是再while语句加了判断条件(str=getchar()!='\n';)****/
        {
                if(str>='A'&&str<='Z'||str>='a'&&str<='z')
                {
                        letters++;
                }
                else if(str==' ')
                        {
                                space++;
                        }
                else if(str>='0'&&str<='9')
                {
          digit++;
                }
                else
                        others++;
                }
        printf("%d %d %d %d",letters,space,others,digit);
        }
/*****我这样写的话,问题出在哪里了,求大佬解答一下,跪谢******/
最佳答案
2021-8-10 14:24:35
张三ccccccc 发表于 2021-8-10 13:48
那那那那getchar()函数不是一次只能输入一个字符,这个题目用getchar()为啥能输入一段字符串,赋值给 ...
  1. #include "stdio.h"
  2. int  main()
  3. {
  4.     char str[10000]={'\0'};
  5.     char *p=str;
  6.     int letters=0,space=0,digit=0,others=0;
  7.     printf("Please input character:\n");
  8.     scanf("%[^\n]",p);
  9.     while (*p!='\0')
  10.     {
  11.             if((*p>='A' && *p<='Z') || (*p>='a' && *p<='z'))
  12.             {
  13.                 letters++;
  14.             }
  15.             else if(*p==' ')
  16.             {
  17.                 space++;
  18.             }
  19.             else if(*p>='0' && *p<='9')
  20.             {
  21.                 digit++;
  22.             }
  23.             else
  24.                 others++;
  25.             p++;
  26.     }
  27.     printf("字符=%d  空格=%d 其他=%d 数字=%d \n",letters,space,others,digit);
  28. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-10 13:40:58 From FishC Mobile | 显示全部楼层
char str这是单字符,无法容纳字符串
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-10 13:48:38 | 显示全部楼层
wp231957 发表于 2021-8-10 13:40
char str这是单字符,无法容纳字符串

那那那那getchar()函数不是一次只能输入一个字符,这个题目用getchar()为啥能输入一段字符串,赋值给str。/跪谢/
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-10 14:24:35 | 显示全部楼层    本楼为最佳答案   
张三ccccccc 发表于 2021-8-10 13:48
那那那那getchar()函数不是一次只能输入一个字符,这个题目用getchar()为啥能输入一段字符串,赋值给 ...
  1. #include "stdio.h"
  2. int  main()
  3. {
  4.     char str[10000]={'\0'};
  5.     char *p=str;
  6.     int letters=0,space=0,digit=0,others=0;
  7.     printf("Please input character:\n");
  8.     scanf("%[^\n]",p);
  9.     while (*p!='\0')
  10.     {
  11.             if((*p>='A' && *p<='Z') || (*p>='a' && *p<='z'))
  12.             {
  13.                 letters++;
  14.             }
  15.             else if(*p==' ')
  16.             {
  17.                 space++;
  18.             }
  19.             else if(*p>='0' && *p<='9')
  20.             {
  21.                 digit++;
  22.             }
  23.             else
  24.                 others++;
  25.             p++;
  26.     }
  27.     printf("字符=%d  空格=%d 其他=%d 数字=%d \n",letters,space,others,digit);
  28. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 22:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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