NO.26十六届蓝桥杯备战|字符数组七道练习|islower|isupper|tolower|toupper|strstr(C++)

news2025/3/13 3:50:40
P5733 【深基6.例1】自动修正 - 洛谷

![[Pasted image 20250306192715.png]]

小写字母 - 32 = 大写字母
大写字母 + 32 = 小写字母

#include <bits/stdc++.h>
using namespace std;

const int N = 110;
char a[N] = { 0 };

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> a;

    int i = 0;
    while (a[i] != '\0')
    { 
        if (a[i] >= 'a' && a[i] <= 'z')
        {
            a[i] -= 32;
        }
        cout << a[i];
        i++;
    }
    cout << endl;
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
char a[N] = { 0 };

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> a;

	int len = strlen(a);
    int i = 0;
    for (i = 0; i < len; i++)
    { 
        if (a[i] >= 'a' && a[i] <= 'z')
        {
            a[i] -= 32;
        }
    }
    cout << a << endl;
    
    return 0;
}

这⾥再给⼤家介绍两个函数: islower 和 tolower ,需要的头⽂件是 <cctype>
字符分类函数和字符转换函数:https://legacy.cplusplus.com/reference/cctype/

int islower ( int c ); //判断字符是否是⼩写字⺟  
int tolower ( int c ); //转换成⼩写字⺟

islower 是C/C++中提供的⼀个判断字符是否是⼩写字⺟的函数,如果参数 c 是⼩写字⺟,函数返回⼀个⾮0的数字,如果不是⼩写字⺟,函数返回0,其实还有⼀个函数是 isupper ,是判断⼤写字⺟的。
tolower 是C/C++中提供的⼀个将参数 c 从⼤写字⺟转化成⼩写字⺟的函数,通过返回值返回转换后的⼩写字⺟。如果 c 本⾝就是⼩写字⺟,则什么都不发⽣。还有⼀个函数是 toupper ,是⼩写字⺟转换成⼤写的。

#include <iostream>  
#include <cctype>  

using namespace std;  
const int N = 110;  
char s[N];  
int main()  
{  
	cin >> s;
	  
	for(int i = 0; s[i] != '\0'; i++)  
	{  
		if(islower(s[i]))  
		{  
			s[i] = toupper(s[i]);  
		}  
	}  
	cout << s <<endl;  

	return 0;
}

B2109 统计数字字符个数 - 洛谷
#include <bits/stdc++.h>
using namespace std;

const int N = 265;
char a[N] = {0};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    fgets(a, 265, stdin);  //会一直读到\n,将\n放入到数组末尾,再加上\0
    int i = 0;
    int cnt = 0;
    while (a[i] != '\n')
    {
        if (a[i] >= '0' && a[i] <= '9')
            cnt++;
        i++;        
    }
    cout << cnt << endl;
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int N = 265;
char a[N] = {0};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    scanf("%[^\n]s", a);  //不会读取\n,会放上\0
    int i = 0;
    int cnt = 0;
    while (a[i] != '\0')
    {
        if (a[i] >= '0' && a[i] <= '9')
            cnt++;
        i++;        
    }
    cout << cnt << endl;
    
    return 0;
}

判断⼀个字符是否是数字字符有⼀个函数是 isdigit ,可以直接使⽤。

int isdigit ( int c );

如果参数 c 是数字字符,则返回⾮ 0 的值,如果不是数字字符,则返回 0 。

#include <iostream>  
#include <cctype>  

using namespace std;  
const int N = 266;  
char arr[N];  

int main()  
{  
	//下⾯这种读取⽅式遇到\n就停⽌,不会讲\n存⼊arr,会⾃动在末尾存放\0  
	scanf("%[^\n]s", arr);  
	int i = 0;  
	int c = 0;  
	while (arr[i] != '\0') //这⾥判断是否等于\0,来觉得是否结束  
	{  
		if (isdigit(arr[i]))  
			c++;  
		i++;  
	}  
	cout << c << endl;  
	
	return 0;  
}
整理药名
#include <bits/stdc++.h>
using namespace std;

const int N = 20;
char a[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	cin >> n;
	while (n--)
	{
		cin >> a;
		if (islower(a[0]))
			a[0] = toupper(a[0]);
		int i = 1;
		while (a[i] != '\0')
		{
			if (isupper(a[i]))
				a[i] = tolower(a[i]);
			i++;
		}
		cout << a << endl;
	}
	

	return 0;
}
B2111 基因相关性 - 洛谷
#include <bits/stdc++.h>
using namespace std;

const int N = 510;
char a[N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    double n = 0;
    cin >> n;
    cin >> a;
    int i = 0;
    char t = 0;
    int cnt = 0;
    while (a[i] != '\0')
    {
        cin >> t;
        if (a[i] == t)
            cnt++;
        i++;
    }
    if (cnt * 1.0 / strlen(a) >= n)
        cout << "yes" << endl;
    else
        cout << "no" << endl;

    return 0;
}
B2113 输出亲朋字符串 - 洛谷
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
char a[N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> a; //a的末尾是\0
    int i = 0;
    while (a[i+1])
    {
        char tmp = a[i] + a[i+1];        
        cout << tmp;
        i++;
    }
    cout << (char)(a[i] + a[0]) << endl;
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
char a[N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> a; //a的末尾是\0
    int i = 0;
    int len = strlen(a);
    while (a[i])
    {
        char tmp = a[i] + a[(i+1) % len];        
        cout << tmp;
        i++;
    }
    
    return 0;
}
  1. 这类题⽬就是精确的控制下标,防⽌越界,算准下标
  2. cout 在打印数据的时候是需要明确知道打印数据的类型的,⽅法1中 str[i] + str[0] 算的结果直接打印就被编译器当做整数打印了,所以我们做了 (char) 的强制类型转换。
B2118 验证子串 - 洛谷
#include <bits/stdc++.h>
using namespace std;

const int N = 25;
char a[N];
char b[N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> a >> b;
    if (strstr(a, b))
        cout << b << " is substring of " << a << endl;
    else if (strstr(b, a))
        cout << a << " is substring of " << b << endl;
    else
        cout << "No substring" << endl;
    
    return 0;
}

这个题⽬使⽤了 strstr 函数,这个函数需要 <cstring> 的头⽂件。

const char * strstr ( const char * str1, const char * str2 );  

这个函数可以查找str1字符串中str2字符串第⼀次出现的位置,如果能找到就返回第⼀次出现的地址,如果找不到则返回 NULL , NULL 的本质是 0 。

B2110 找第一个只出现一次的字符 - 洛谷

暴力统计

#include <bits/stdc++.h>
using namespace std;

const int N = 1110;
char a[N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> a;
    int i = 0;
	while (a[i])
    {
        int cnt = 0;
        int j = 0;
        while (a[j])
        {
            if (a[i] == a[j])
                cnt++;
            j++;        
        }
        if (cnt == 1)
        {
            cout << a[i];
            break;
        }
        i++;
    }
    if (a[i] == 0)
        cout << "no" << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int N = 1110;
char a[N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> a;
    int i = 0;
    int flag = 0;
    while (a[i])
    {
        int cnt = 0;
        int j = 0;
        while (a[j])
        {
            if (a[i] == a[j])
                cnt++;
            j++;        
        }
        if (cnt == 1)
        {
            cout << a[i];
            flag = 1;
            break;
        }
        i++;
    }
    if (flag == 0)
        cout << "no" << endl;
    return 0;
}

哈希
题⽬说字符串中只有⼩写字⺟,⼩写字⺟的ASCII值的范围是:97122,根据前⾯我们学习的知识,我们知道在C和C++中每个字符都有ASCII值,标准的ASCII码表中有128个字符,ASCII值的范围是0127.
所以我们创建⼀个128元素的整型数组,下标分别是0~127,下标正好和字符的ASCII值范围对应,那么整型的数组的⼀个元素就为⼀个字符计数就可以。每读取⼀个字符,根据字符的ASCII值将数组中下标为字符ASCII值的元素值+1,相当于统计这个字符出现的次数。
![[Pasted image 20250306191825.png]]

#include <bits/stdc++.h>
using namespace std;

const int N = 1110;
char a[N];
int n[128];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> a;
    int i = 0;
    while (a[i])
    {
        n[a[i]]++;
        i++;        
    }
    i = 0;
    while (a[i])
    {
        if(n[a[i]] == 1)
        {
            cout << a[i] << endl;
            break;
        }
        i++;
    }
    if (a[i] == 0)
        cout << "no" << endl;
    
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int N = 1110;
char a[N];
int n[26];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> a;
    int i = 0;
    while (a[i])
    {
        n[a[i] - 'a']++;
        i++;        
    }
    i = 0;
    while (a[i])
    {
        if(n[a[i] - 'a'] == 1)
        {
            cout << a[i] << endl;
            break;
        }
        i++;
    }
    if (a[i] == 0)
        cout << "no" << endl;
    
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2311508.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据安全VS创作自由:ChatGPT与国产AI工具隐私管理对比——论文党程序员必看的避坑指南

文章目录 数据安全VS创作自由&#xff1a;ChatGPT与国产AI工具隐私管理对比——论文党程序员必看的避坑指南ChatGPTKimi腾讯元宝DeepSeek 数据安全VS创作自由&#xff1a;ChatGPT与国产AI工具隐私管理对比——论文党程序员必看的避坑指南 产品隐私设置操作路径隐私协议ChatGPT…

乐鑫打造全球首款 PSA Certified Level 2 RISC-V 芯片

乐鑫科技 (688018.SH) 荣幸宣布 ESP32-C6 于 2025 年 2 月 20 日获得 PSA Certified Level 2 认证。这一重要突破使 ESP32-C6 成为全球首款基于 RISC-V 架构获此认证的芯片&#xff0c;体现了乐鑫致力于为全球客户提供安全可靠、性能卓越的物联网解决方案的坚定承诺。 PSA 安全…

Flink深入浅出之03:状态、窗口、checkpoint、两阶段提交

Flink是一个有状态的流&#xff0c;&#x1f445;一起深入了解这个有状态的流 3️⃣ 目标 掌握State知识掌握Flink三种State Backend掌握Flink checkpoint和savepoint原理了解Flink的重启策略checkpointtwo phase commit保证E-O语义 4️⃣ 要点 &#x1f4d6; 1. Flink的St…

DeepSeek 助力 Vue3 开发:打造丝滑的表格(Table)示例3: 行选择

前言&#xff1a;哈喽&#xff0c;大家好&#xff0c;今天给大家分享一篇文章&#xff01;并提供具体代码帮助大家深入理解&#xff0c;彻底掌握&#xff01;创作不易&#xff0c;如果能帮助到大家或者给大家一些灵感和启发&#xff0c;欢迎收藏关注哦 &#x1f495; 目录 Deep…

Django下防御Race Condition

目录 漏洞原因 环境搭建 复现 A.无锁无事务时的竞争攻击 B.无锁有事务时的竞争攻击 防御 A.悲观锁加事务防御 B.乐观锁加事务防御 总结 漏洞原因 Race Condition 发生在多个执行实体&#xff08;如线程、进程&#xff09;同时访问共享资源时&#xff0c;由于执行顺序…

失踪人口回归,最近接了一个私活,提升了很多。

上图是本项目用到的所有技术栈 这个项目分为四端(前端) App(只做安卓不上架) 技术栈ReactNative TS Socket.io scss桌面端(只做Win) 技术栈 Electron TS Vue3 Socket.ioweb端技术栈 Vue3 TS ElementPlus Day.js Unocss Vite Axios Pinia Md5 Echarts less小程序技术栈 Uniapp…

HarmonyOS 应用程序包结构 (编译态)

不同类型的Module编译后会生成对应的HAP、HAR、HSP等文件&#xff0c;开发态视图与编译态视图的对照关系如下&#xff1a; 从开发态到编译态&#xff0c;Module中的文件会发生如下变更&#xff1a; ets目录&#xff1a;ArkTS源码编译生成.abc文件。resources目录&#xff1a;A…

原生iOS集成react-native (react-native 0.65+)

由于官方文档比较老&#xff0c;很多配置都不能用&#xff0c;集成的时候遇到很多坑&#xff0c;简单的整理一下 时间节点:2021年9月1日 本文主要提供一些配置信息以及错误信息解决方案&#xff0c;具体步骤可以参照官方文档 原版文档&#xff1a;https://reactnative.dev/docs…

Doris vs ClickHouse 企业级实时分析引擎怎么选?

Apache Doris 与 ClickHouse 同作为OLAP领域的佼佼者&#xff0c;在企业级实时分析引擎该如何选择呢。本文将详细介绍 Doris 的优势&#xff0c;并通过直观对比展示两者的关键差异&#xff0c;同时分享一个企业成功用 Doris 替换 ClickHouse 的实践案例&#xff0c;帮助您做出明…

【Multipath】使用(FC)访问远程存储设备

文章目录 一、硬件与环境准备二、扫描设备1.宽幅扫描2.窄幅扫描&#xff1a;根据HCTL去扫3.查看远程端口&#xff08;第一次扫描后会出现&#xff09;4.查看FC远程存储设备软链接&#xff08;块设备&#xff09;5.根据HCTL查看FC块设备6.根据块设备wwn查找多路径设备 一、硬件与…

豆包大模型 MarsCode AI 刷题专栏 001

001.找单独的数 难度&#xff1a;易 问题描述 在一个班级中&#xff0c;每位同学都拿到了一张卡片&#xff0c;上面有一个整数。有趣的是&#xff0c;除了一个数字之外&#xff0c;所有的数字都恰好出现了两次。现在需要你帮助班长小C快速找到那个拿了独特数字卡片的同学手上…

用Ruby的Faraday库来进行网络请求抓取数据

在 Ruby 中&#xff0c;Faraday 是一个非常强大的 HTTP 客户端库&#xff0c;它可以用于发送 HTTP 请求并处理响应。你可以使用 Faraday 来抓取网页数据&#xff0c;处理 API 请求等任务。下面我将向你展示如何使用 Faraday 库进行网络请求&#xff0c;抓取数据并处理响应。 1.…

计算机视觉深度学习入门(2)

卷积运算 Dense层与卷积层的根本区别在于&#xff0c;Dense层从输入特征空间中学到的是全局模式&#xff08;比如对于MNIST数字&#xff0c;全局模式就是涉及所有像素的模式&#xff09;​&#xff0c;而卷积层学到的是局部模式&#xff08;对于图像来说**&#xff0c;局部模式…

计算机毕业设计Python+DeepSeek-R1大模型医疗问答系统 知识图谱健康膳食推荐系统 食谱推荐系统 医疗大数据(源码+LW文档+PPT+讲解)

温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 温馨提示&#xff1a;文末有 CSDN 平台官方提供的学长联系方式的名片&#xff01; 作者简介&#xff1a;Java领…

nginx服务器实现上传文件功能_使用nginx-upload-module模块

目录 conf文件内容如下html文件内容如下上传文件功能展示 conf文件内容如下 #user nobody; worker_processes 1;error_log /usr/logs/error.log; #error_log /usr/logs/error.log notice; #error_log /usr/logs/error.log info;#pid /usr/logs/nginx.pid;even…

ReferenceError: assignment to undeclared variable xxx

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》、《前端求职突破计划》 &#x1f35a; 蓝桥云课签约作者、…

im即时聊天客服系统SaaS还是私有化部署:成本、安全与定制化的权衡策略

随着即时通讯技术的不断发展&#xff0c;IM即时聊天客服系统已经成为企业与客户沟通、解决问题、提升用户体验的重要工具。在选择IM即时聊天客服系统时&#xff0c;企业面临一个重要决策&#xff1a;选择SaaS&#xff08;软件即服务&#xff09;解决方案&#xff0c;还是进行私…

深入理解与配置 Nginx TCP 日志输出

一、背景介绍 在现代网络架构中&#xff0c;Nginx 作为一款高性能的 Web 服务器和反向代理服务器&#xff0c;广泛应用于各种场景。除了对 HTTP/HTTPS 协议的出色支持&#xff0c;Nginx 从 1.9.0 版本开始引入了对 TCP 和 UDP 协议的代理功能&#xff0c;这使得它在处理数据库…

【文心索引】搜索引擎测试报告

目录 一、项目背景 1、互联网信息爆炸的时代背景 2、搜索引擎的应运而生 3、搜索引擎的市场需求和竞争态势 4、搜索引擎项目的意义 二、项目功能 1、基础搜索功能 2、用户交互与体验功能 3、数据索引与爬取功能 三、测试报告 3.1.功能测试 3.1.1.输入测试&#xff…

ReAct论文阅读笔记总结

ReAct&#xff1a;Synergizing Reasoning and Acting in Language Models 背景 最近的研究结果暗示了在自主系统中结合语言推理与交互决策的可能性。 一方面&#xff0c;经过适当Prompt的大型语言模型&#xff08;LLMs&#xff09;已经展示了在算术、常识和符号推理任务中通…