鱼C论坛

 找回密码
 立即注册
查看: 753|回复: 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()为啥能输入一段字符串,赋值给 ...
#include "stdio.h"
int  main()
{
    char str[10000]={'\0'};
    char *p=str;
    int letters=0,space=0,digit=0,others=0;
    printf("Please input character:\n");
    scanf("%[^\n]",p);
    while (*p!='\0')
    {
            if((*p>='A' && *p<='Z') || (*p>='a' && *p<='z'))
            {
                letters++;
            }
            else if(*p==' ')
            {
                space++;
            }
            else if(*p>='0' && *p<='9')
            {
                digit++;
            }
            else
                others++;
            p++;
    }
    printf("字符=%d  空格=%d 其他=%d 数字=%d \n",letters,space,others,digit);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-10 13:40:58 From FishC Mobile | 显示全部楼层
char str这是单字符,无法容纳字符串
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

那那那那getchar()函数不是一次只能输入一个字符,这个题目用getchar()为啥能输入一段字符串,赋值给str。/跪谢/
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-10 14:24:35 | 显示全部楼层    本楼为最佳答案   
张三ccccccc 发表于 2021-8-10 13:48
那那那那getchar()函数不是一次只能输入一个字符,这个题目用getchar()为啥能输入一段字符串,赋值给 ...
#include "stdio.h"
int  main()
{
    char str[10000]={'\0'};
    char *p=str;
    int letters=0,space=0,digit=0,others=0;
    printf("Please input character:\n");
    scanf("%[^\n]",p);
    while (*p!='\0')
    {
            if((*p>='A' && *p<='Z') || (*p>='a' && *p<='z'))
            {
                letters++;
            }
            else if(*p==' ')
            {
                space++;
            }
            else if(*p>='0' && *p<='9')
            {
                digit++;
            }
            else
                others++;
            p++;
    }
    printf("字符=%d  空格=%d 其他=%d 数字=%d \n",letters,space,others,digit);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 22:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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