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

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;
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

笔记四:C语言中的文件和文件操作

Faye&#xff1a;只要有正确的伴奏&#xff0c;什么都能变成好旋律。 ---------《寻找天堂》 目录 一、文件介绍 1.1程序文件 1.2 数据文件 1.3 文件名 二、文件的打开和关闭 2.1 文件指针 2.2.文件的打开和关闭 2.3 文件读取结束的判定 三、 文件的顺序读写 3.1 顺序读写…

DeepSeek进阶应用(一):结合Mermaid绘图(流程图、时序图、类图、状态图、甘特图、饼图)

&#x1f31f;前言: 在软件开发、项目管理和系统设计等领域&#xff0c;图表是表达复杂信息的有效工具。随着AI助手如DeepSeek的普及&#xff0c;我们现在可以更轻松地创建各种专业图表。 名人说&#xff1a;博观而约取&#xff0c;厚积而薄发。——苏轼《稼说送张琥》 创作者&…

【OneAPI】网页截图API-V2

API简介 生成指定URL的网页截图或缩略图。 旧版本请参考&#xff1a;网页截图 V2版本新增全屏截图、带壳截图等功能&#xff0c;并修复了一些已知问题。 全屏截图&#xff1a; 支持全屏截图&#xff0c;通过设置fullscreentrue来支持全屏截图。全屏模式下&#xff0c;系统…

基于SpringBoot的餐厅点餐管理系统设计与实现(源码+SQL脚本+LW+部署讲解等)

专注于大学生项目实战开发,讲解,毕业答疑辅导&#xff0c;欢迎高校老师/同行前辈交流合作✌。 技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、小程序、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;…

Windows Server 2022:赋能未来,打造智能高效的企业数字基座---免费下载

免费下载地址 Windows Server 2022&#xff1a;赋能未来&#xff0c;打造智能高效的企业数字基座‌ 在数字化转型的浪潮中&#xff0c;企业需要更安全、更灵活、更智能的基础设施支撑。‌Windows Server 2022‌作为微软新一代服务器操作系统&#xff0c;以革新性的技术架构和行…

支持向量简要理解

决策方程符合感知机区分理论&#xff0c;我们基于线性代数来看这满足子空间理论&#xff0c;可以获取得到超平面。 支持向量机的目标是寻找最与超平面最近的点的最大距离&#xff0c;而距离计算如上&#xff0c;符合数学上计算点到线&#xff08;面&#xff09;的距离公式。 …

USB2.0 学习(1)字段和包

目录 1 字段 1.1 包识别字段PID 1.2 地址字段 1.3帧号字段 1.4 数据字段 1.5 CRC字段 2 包 2.1令牌包 2.2帧起始包 2.3数据包 2.4SPLIT包(分割事务包) 2.5握手包 参考 USB包的构成是一个逐层的过程,首先这些串行数据按照特定的规则构成字段,字段是构成包的基本…

AI 人工智能深度解析:从基础到前沿,全面掌握未来科技

AI 人工智能深度解析&#xff1a;从基础到前沿&#xff0c;全面掌握未来科技 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;可以分享一下给大家。点击跳转到网站。 https://www.captainbed.cn/ccc 文章目录 AI 人工智能深度解析…

2025-03-09 学习记录--C/C++-PTA 习题11-1 输出月份英文名

合抱之木&#xff0c;生于毫末&#xff1b;九层之台&#xff0c;起于累土&#xff1b;千里之行&#xff0c;始于足下。&#x1f4aa;&#x1f3fb; 一、题目描述 ⭐️ 裁判测试程序样例&#xff1a; #include <stdio.h>char *getmonth( int n );int main() {int n;char …

【音视频 | AAC】AAC编码库faac介绍、使用步骤、例子代码

&#x1f601;博客主页&#x1f601;&#xff1a;&#x1f680;https://blog.csdn.net/wkd_007&#x1f680; &#x1f911;博客内容&#x1f911;&#xff1a;&#x1f36d;嵌入式开发、Linux、C语言、C、数据结构、音视频&#x1f36d; &#x1f923;本文内容&#x1f923;&a…

macos 程序 运行

sudo xattr -r -d com.apple.quarantine [/Applications/Name]使用stow 管理配置文件

JavaWeb后端基础(7)AOP

AOP是Spring框架的核心之一&#xff0c;那什么是AOP&#xff1f;AOP&#xff1a;Aspect Oriented Programming&#xff08;面向切面编程、面向方面编程&#xff09;&#xff0c;其实说白了&#xff0c;面向切面编程就是面向特定方法编程。AOP是一种思想&#xff0c;而在Spring框…

STM32驱动OLED屏幕全解析:从原理到温度显示实战(上) | 零基础入门STM32第五十三步

主题内容教学目的/扩展视频OLED显示屏重点课程电路原理&#xff0c;手册分析&#xff0c;驱动程序。初始化&#xff0c;清屏&#xff0c;ASCII字库&#xff0c;显示分区。调用显示函数。做带有加入图形和汉字显示的RTC时钟界面。讲字库的设计原理。 师从洋桃电子&#xff0c;杜…

基于YOLO11深度学习的运动品牌LOGO检测与识别系统【python源码+Pyqt5界面+数据集+训练代码】

《------往期经典推荐------》 一、AI应用软件开发实战专栏【链接】 项目名称项目名称1.【人脸识别与管理系统开发】2.【车牌识别与自动收费管理系统开发】3.【手势识别系统开发】4.【人脸面部活体检测系统开发】5.【图片风格快速迁移软件开发】6.【人脸表表情识别系统】7.【…

ctfshow做题笔记—栈溢出—pwn65~pwn68

目录 前言 一、pwn65(你是一个好人) 二、pwn66(简单的shellcode&#xff1f;不对劲&#xff0c;十分得有十二分的不对劲) 三、pwn67(32bit nop sled)&#xff08;确实不会&#xff09; 四、pwn68(64bit nop sled) 前言 做起来比较吃力哈哈&#xff0c;自己还是太菜了&…

【新手指南】pyqt可视化远程部署deepseek7B蒸馏版模型

本地效果&#xff1a;&#xff08;如果想做这个的本科毕设&#xff0c;建议美化界面。&#xff09; 总结&#xff1a;MobaXterm远程连接autodl服务器&#xff0c;在MobaXterm上利用X11转发使pyqt可视化页面在自己的电脑上展现出来。 1. 官网下载MobaXterm MobaXterm free Xse…

SpringBoot(一)--搭建架构5种方法

目录 一、⭐Idea从spring官网下载打开 2021版本idea 1.打开创建项目 2.修改pom.xml文件里的版本号 2017版本idea 二、从spring官网下载再用idea打开 三、Idea从阿里云的官网下载打开 ​编辑 四、Maven项目改造成springboot项目 五、从阿里云官网下载再用idea打开 Spri…

DeepSeek×博云AIOS:突破算力桎梏,开启AI普惠新纪元

背景 在全球人工智能技术高速迭代的背景下&#xff0c;算力成本高企、异构资源适配复杂、模型部署效率低下等问题&#xff0c;始终是制约企业AI规模化应用的关键。 DeepSeek以创新技术直击产业痛点&#xff0c;而博云先进算力管理平台AIOS的全面适配&#xff0c;则为这一技术…

JVM垃圾回收面试题及原理

1. 对象什么时候可以被垃圾器回收 如果一个或多个对象没有任何的引用指向它了&#xff0c;那么这个对象现在就是垃圾&#xff0c;如果定位了垃圾&#xff0c;则有可能会被垃圾回收器回收 如果要定位什么是垃圾&#xff0c;有两种方式来确定 引用计数法可达性分析算法 1.1 …

计算机视觉算法实战——老虎个体识别(主页有源码)

✨个人主页欢迎您的访问 ✨期待您的三连 ✨ ✨个人主页欢迎您的访问 ✨期待您的三连 ✨ ✨个人主页欢迎您的访问 ✨期待您的三连✨ ​ ​​​ 1. 领域介绍 老虎个体识别是计算机视觉中的一个重要应用领域&#xff0c;旨在通过分析老虎的独特条纹图案&#xff0c;自动识别和区…