C++统计字符串中各类型字符的个数

2023-05-16

#include <iostream>
#include <cstring>
using namespace std;

int main() {

    int letter = 0;
    int digit = 0;
    int space = 0;
    int other = 0;
    
    char buf[1024] = {0};
    cin.getline(buf, sizeof(buf));

    // write your code here......
    for(int i = 0; buf[i] != '\0'; i++){ //遍历字符串到'\0'为止
        char c = buf[i];
        if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') //大小写字母
            letter++;
        else if(c >= '0' && c <= '9') //数字
            digit++;
        else if(c == ' ')  //空格
            space++; 
        else //其他字符
            other++;
    }
    cout << "letter:" << letter << " digit:" << digit << " space:" << space << " other:" << other << endl;

	return 0;
}

#include <iostream>
#include <cstring>
using namespace std;

int main() {

    int letter = 0;
    int digit = 0;
    int space = 0;
    int other = 0;
    
    char buf[1024] = {0};
    cin.getline(buf, sizeof(buf));

    // write your code here......
    int len = strlen(buf);
    for(int i = 0;i < len; i++){
        if(isalpha(buf[i])){
            letter++;
        }else if(isdigit(buf[i])){
            digit++;
        }else if(isspace(buf[i])){
            space++;
        }else{
            other++;
        }
    }
    cout << "letter:" << letter << " digit:" << digit << " space:" << space << " other:" << other << endl;

	return 0;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++统计字符串中各类型字符的个数 的相关文章

随机推荐

  • 第十三周作业-必做3

    题目描述 xff1a 在大家不辞辛劳的帮助下 xff0c TT 顺利地完成了所有的神秘任务 神秘人很高兴 xff0c 决定给 TT 一个奖励 xff0c 即白日做梦之捡猫咪游戏 捡猫咪游戏是这样的 xff0c 猫咪从天上往下掉 xff0c
  • CodeForces 1165-B Polycarp Training

    题目 传送门 思路 将所有比赛进项排序 对于 第k天 xff0c 我们从贪心的角度出发肯定要选最接近 k 题的 比赛 不能比k题小 这样的话第 k 1天所选比赛的题数小于等于 k 天 的比赛题数 xff0c 所以我们的这个方法的复杂度是线性
  • gym 102302 2019 USP-ICMC H-Log Concave Sequences (dp + 矩阵快速幂优化)

    题目 传送门 思路 我们可以先写出转移方程 xff0c 发现该方程是一个不变的递推式 我们考虑用矩阵快速幂来优化这个递推式 完结撒花 AC Code span class token macro property span class to
  • Maven配置打包的jar或者war文件到指定目录

    最近项目打包比较频繁 xff0c 而且使用maven打包之后生成的jar包文件的都在不同项目的根目录的target目录下 xff0c 项目发布时候来回拷贝 xff0c 着实蛋疼 xff0c 所以就考虑把所有的项目到集中打包到一个目录里面 x
  • windows远程桌面连接树莓派通过xrdp服务

    远程桌面协议 xff08 RDP xff09 是微软的专有协议 xff0c 它利用低带宽连接来提供对桌面的访问 为了允许在树莓派上使用RDP xff0c 我们将使用一个名为xrdp的软件 xrdp软件将你的屏幕和格式化为他们的RDP实现 在
  • windows下Anaconda更改默认python环境的方法

    windows Linux下Anaconda更改默认python环境的方法 更改anaconda安装目录下 anaconda3 Scripts activate bat文件 将第24行 span class token decorator
  • 文献管理软件Zotero常用插件安装及配置使用

    文献管理软件 Zotero常用插件安装及配置使用 一 Zotero安装与同步盘配置1 下载Zotero并安装2 配置Zotero xff08 1 xff09 配置同步盘 xff08 以onedrive为例 xff09 如果不配置同步盘 xf
  • Github本地仓库使用学习记录

    一 注册Github账号 在官网注册github的账号 xff1a https github com 二 下载git本地客户端并安装 Windows 三个平台下载地址 xff1a http git scm com downloads 国内的
  • Win10+GTX 1660 SUPER安装Cuda11.5.1+cudnn8.3.0

    Win10 43 GTX 1660 SUPER安装Cuda11 5 1 43 cudnn8 3 0 一 cuda11 5 1安装步骤1 查看电脑的显卡驱动2 下载显卡驱动3 下载需要的cuda版本 二 对应版本Cudnn安装1 注册nvid
  • python的列表数据写入excel表

    python的列表数据写入excel表 将python代码生成的一个列表数值导入到excel发现按照行列排列不能全部输出到excel表的一列当中 xff0c 查阅资料后发现可以用下面的函数进行写入 span class token keyw
  • 最新zotero与obsidian笔记联动教程(可代替citations和mdnotes)

    最新zotero与obsidian笔记联动教程 xff08 可代替citations和mdnotes xff09 一 联动原理二 插件配置1 zotero better bibtex2 Bibnotes Formatter3 MarkDBC
  • 第十四周作业-必做2

    题目描述 xff1a Q老师 得到一张 n 行 m 列的网格图 xff0c 上面每一个格子要么是白色的要么是黑色的 Q老师认为失去了 十字叉 的网格图莫得灵魂 一个十字叉可以用一个数对 x 和 y 来表示 其中 1 x n 并且 1 y m
  • Ubuntu18.04系统及相关软件安装恢复过程

    Ubuntu18 04系统及相关软件安装恢复过程 一 常用软件安装1 系统安装 https blog csdn net qq 43309940 article details 116656810 2 显卡驱动安装 https blog cs
  • mujoco安装及urdf转xml方法记录

    参考 mujoco210及mujoco py安装 下载适用于Linux或 OSX的 MuJoCo 2 1 版二进制文件 将mujoco210的下载的目录解压到 mujoco mujoco210路径下 注意 xff1a 如果要为包指定非标准位
  • 多台虚拟机实现局域网内互连

    Step1 xff1a 要在同一局域网内连接上对方的虚拟机 xff0c 就要相应地使用VMware的桥接模式 xff0c 以桥接到这一局域网的网卡上 这里以我连接的学校的局域网为例 xff1a Step2 xff1a 打开VMware的虚拟
  • C++判断成绩等级

    else if span class token macro property span class token directive hash span span class token directive keyword include
  • C++判断一个数是不是质数

    详见及参考 xff1a https www nowcoder com practice b8bb5e7703da4a83ac7754c0f3d45a82 tpId 61 225 amp tags 61 amp title 61 amp di
  • C++比较字符串大小(自己实现strcmp()函数)

    详见 xff1a https www nowcoder com practice 963e455fdf7c4a4a997160abedc1951b tpId 61 225 amp tags 61 amp title 61 amp diffi
  • sql多表查询之合并查询(union)

    题1 https www nowcoder com practice 203d0aed8928429a8978185d9a03babc tpId 61 240 amp tags 61 amp title 61 amp difficulty
  • C++统计字符串中各类型字符的个数

    span class token macro property span class token directive hash span span class token directive keyword include span spa