C++中字符串大小写字母转换

article/2025/10/15 7:09:36

最近在学习 STL,string 也是 STL 中的一种容器,遇到一个字符串中字母大小写转换的例子,这里就顺便总结一下在C++中常用的字符串大小写转换方法,有需要的可以参考。代码如下:

1、char[]类型,调用库函数
//==========================================										   	
//	Filename : 1、char[]类型,调用库函数						   	
//	Time     : 2019年5月6日					   
//	Authonr  : 柚子树					   	
//	Email    : gz_duyong@163.com		   										   
//==========================================#include <iostream>
#include <string>
#include <cstdio>using namespace std;void myToupper(char* str)
{int length = strlen(str);for (size_t i = 0; i < length; i++){if (str[i] >= 'a' && str[i] <= 'z'){str[i] = toupper(str[i]);}}
}void myTolower(char* str)
{int length = strlen(str);for (size_t i = 0; i < length; i++){if (str[i] >= 'A' && str[i] <= 'Z'){str[i] = tolower(str[i]);}}
}int main()
{char str[20] = { 0 };cout << "请输入一个包含大小写字母的字符串: " << endl;cin.getline(str, 20);myTolower(str);cout << "转小写: " << str << endl;myToupper(str);cout << "转大写: " << str << endl;system("pause");return EXIT_SUCCESS;
}

运行结果:
在这里插入图片描述

2、char[]类型,自定义转换函数
//==========================================										   	
//	Filename : 2、char[]类型,自定义转换函数					   	
//	Time     : 2019年5月6日
//	Authonr  : 柚子树					   	
//	Email    : gz_duyong@163.com		   										   
//==========================================#include <iostream>
#include <string>
#include <cmath>
using namespace std;void myToupper(char* str)
{int length = strlen(str);for (size_t i = 0; i < length; i++){if (str[i] >= 'a' && str[i] <= 'z'){str[i] -= 32;// str[i] = str[i] - 'a' + 'A';}}
}void myTolower(char* str)
{int length = strlen(str);for (size_t i = 0; i < length; i++){if (str[i] >= 'A' && str[i] <= 'Z'){str[i] += 32;// str[i] = str[i] - 'A' + 'a';}}
}int main()
{char str[20] = { 0 };cout << "请输入一个包含大小写字母的字符串: " << endl;cin.getline(str, 20);myTolower(str);cout << "转小写: " << str << endl;myToupper(str);cout << "转大写: " << str << endl;system("pause");return EXIT_SUCCESS;
}

运行结果:
在这里插入图片描述

3、string类型,调用库函数
//==========================================							   	
//	Filename : 3、string类型,调用库函数					   	
//	Time     : 2019年5月6日					   
//	Authonr  : 柚子树					   	
//	Email    : gz_duyong@163.com		   										   
//==========================================#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;string str;int main()
{cout << "请输入一个包含大小写字母的字符串: " << endl;cin >> str;transform(str.begin(), str.end(), str.begin(), ::tolower);cout << "转小写: " << str << endl;transform(str.begin(), str.end(), str.begin(), ::toupper);cout << "转大写: " << str << endl;system("pause");return EXIT_SUCCESS;
}

运行结果:
在这里插入图片描述

4、string类型,自定义实现转换
//==========================================										   	
//	Filename : 4、string类型,自定义实现转换					   	
//	Time     : 2019年5月6日					   
//	Authonr  : 柚子树					   	
//	Email    : gz_duyong@163.com		   										   
//==========================================#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;void myToupper(string &str)
{int length = str.size();for (size_t i = 0; i < length; i++){if (str[i] >= 'a' && str[i] <= 'z'){str[i] -= 32;// str[i] = str[i] - 'a' + 'A';}}
}void myTolower(string &str)
{int length = str.size();for (size_t i = 0; i < length; i++){if (str[i] >= 'A' && str[i] <= 'Z'){str[i] += 32;// str[i] = str[i] - 'a' + 'A';}}
}int main()
{string str;cout << "请输入一个包含大小写字母的字符串: " << endl;cin >> str;myTolower(str);cout << "转小写: " << str << endl;myToupper(str);cout << "转大写: " << str << endl;system("pause");return EXIT_SUCCESS;
}

运行结果:
在这里插入图片描述


http://chatgpt.dhexx.cn/article/ogdO2Oq0.shtml

相关文章

大小写字母转换(java大小写字母转换)

苹果4SQQ密码大小写字母怎么转换不&#xff1f;苹果4SQQ密码大小写字母 每次要输密码,当时在电脑上设置的密码,可以改变大小写,但是到了手机上,我不知道怎么换大小写,那位朋友告诉下 苹果手机输入法怎么改字母的大小写 (很高兴为您解答,有帮助请给好评,谢谢! ) 大小写转换-键盘…

英文字母大小写转换

从键盘输入一个大写英文字母&#xff0c;将其转换味小写字母后&#xff0c;再显示到屏幕上&#xff08;显示字符后要输出一个换行&#xff09; 输入格式要求&#xff1a;提示信息&#xff1a;"Press a key and then press Enter:" 程序运行实例如下&#xff1a; P…

Java 字符串中的大小写字母转换

Java 实现字符串中字母大小写转换函数 实现思路&#xff1a;将字符串中的每个字符取出&#xff0c;判断该字符是不是大写字母&#xff0c;如果是的话则该字符32&#xff08;ASCII码&#xff09;并进行强制转换&#xff0c;转换为char&#xff0c;然后在通过字符串的合并操作进…

大写字母转换成小写字母

大写字母转换成小写字母 大写字母转换成小写字母1.示例&#xff1a;2. 解题思路3. 代码展示及分析3.1 ASCII码操作字符串转换大小写3.2 位运算 4. 运行结果 大写字母转换成小写字母 题目&#xff1a; 实现函数 ToLowerCase()&#xff0c;该函数接收一个字符串参数 str&#xf…

C++ | 大小写字母转换

1.题目描述 实现字母的大小写转换。多组输入输出。 输入描述: 多组输入&#xff0c;每一行输入大写字母。 输出描述: 针对每组输入输出对应的小写字母。 答案如下&#xff1a; #include<iostream> using namespace std;int main(){ //----------------------------…

Java-大小写字母转换

题目&#xff1a;输入一段字符&#xff0c;包含大写字母或者小写字母&#xff0c;输出对应的大写或者小写转换&#xff0c;数字不管 代码实现&#xff1a; import java.util.Scanner;public class ZimuChange {public static void main(String[] args) {System.out.println(&qu…

线程中CreateEvent、SetEvent、WaitForSingleObject的用法

原文地址&#xff1a;https://www.cnblogs.com/MrYuan/p/5238749.html 首先介绍CreateEvent是创建windows事件的意思&#xff0c;作用主要用在判断线程退出&#xff0c;线程锁定方面. CreateEvent 函功能描述&#xff1a;创建或打开一个命名的或无名的事件对象. EVENT有两种…

CreateEvent SetEvent WaitForSingleObjec

在自动重置事件对象中&#xff0c;当WaitSingleObject/WaitForMultipleObjects接收到SetEvent发送过来的信号后则返回WAIT_OBJECT_0&#xff0c;此时操作系统&#xff08;待定&#xff09;自动重置等待的事件对象&#xff08;即自动将其设置为无信号状态。无论何时通过SetEvent…

线程中CreateEvent和SetEvent及WaitForSingleObject的用法

首先介绍CreateEvent是创建windows事件的意思&#xff0c;作用主要用在判断线程退出&#xff0c;线程锁定方面. CreateEvent 函功能描述&#xff1a;创建或打开一个命名的或无名的事件对象. EVENT有两种状态&#xff1a;发信号&#xff0c;不发信号。 SetEvent/ResetEvent…

wireshark找不到接口?你的NPF没启动

使用wireshark时会遇到找不到接口的问题&#xff0c;这是因为电脑的NPF没有启动。以管理员身份运行cmd,输入net start npf即可。不过&#xff0c;电脑重启后&#xff0c;它又关闭了。

wireshark抓包报错The capture session could not be initiated on interface '\Device\NPF_Loopback'

wireshark抓包报错 The capture session could not be initiated on interface ‘\Device\NPF_Loopback’ (Error opening adapter: The system cannot find the path specified. (3)). 解决方法 以管理员身份运行cmd&#xff0c;输入net start npcap 重新打开wireshark即可…

The NPF or NPCAP service is not installed, please install Winpcap or Npcap aand reboot的解决方法

安装好GNS启动后&#xff0c;我遇到了GNS3没有加载的错误&#xff0c;并且发出错误声明“未安装NPF或NPCAP服务&#xff0c;请安装Winpcap或Npcap并重启。” 确认已经安装了winpcap&#xff0c;怎么还会报错呢&#xff1f;&#xff1f;后来发现是没有勾选自启动winpcap软件导致…

wireshark提示未启动npf服务The NPF driver isn’t running You may have trouble capturing or listing interfaces

提示未启动npf服务的解决办法 检查你的电脑有没有安装WinPcap&#xff0c;如果没有安装就安装一下以管理员身份运行cmd.exe&#xff0c;执行命令&#xff1a;sc config npf start auto&#xff0c;重启wireshark如果还提示未启动npf服务就执行这个命令&#xff1a;net start n…

解决启动Wireshark时遇到的“The NPF driver isn't running...”

最近在使用wireshark抓包软件的时候遇到了这个问题&#xff0c;在启动wireshark的时候&#xff0c;会弹出一个对话框&#xff1a; 上网搜索了一下&#xff0c;结合自己的想法&#xff0c;得到如下两个解决方法&#xff1a; 一、图形界面操作 1、右击计算机&#xff0c;“管理”…

wireshark/The NPF driver isn’t running./Unable to load WinPcap (wpcap.dll)

很久没使用wireshark后重新打开就出现警告&#xff1a; The NPF driver isn’t running. You may have trouble capturing or listing interfaces. 点击菜单栏caption&#xff0c;准备启动的时候&#xff0c;又显示警告框&#xff0c;直接不给操作&#xff1a; Unable to load …

使用Wireshark抓包软件提示The NPF driver isn’t running解决办法

Wireshark一个强大的数据抓包分析工具&#xff0c;在Win7 64位系统上第一次使用时&#xff0c;可能会出现意外的情况。 The NPF driver isn’t running. 这个情况可能是因为没有安装Winpcap驱动或者安装Winpcap时没有选中开机自动启动winpcap选项。 解决方法&#xff1a; 1. 没…

【西门子】S7-PLCSIM Advance_V2/V3, Error Code: -30,LicenseNotFound /NetGroup Packet Filter Driver (NPF)

最近公司需要测试视觉程序与西门子S7-1500通讯&#xff0c;下载了下西门子的编程程序进行通讯测试&#xff0c;本来想着安装个程序很简单的事&#xff0c;谁知道在使用S7-PLCSIM Advance_V3仿真通讯时遇到各种问题&#xff0c;鉴于自己踩坑太多&#xff0c;特此分享自己的完美解…

wireshark找不到捕获接口问题和net start npf 服务器名无效、拒绝访问的解决办法

win10系统 解决方法&#xff1a; 1、去官网下载用于网络封包抓取的工具 winpcap 链接&#xff1a;https://www.winpcap.org/install/bin/WinPcap_4_1_3.exe 2、用管理员身份运行命令提示符 3、输入net start npf 启动服务 可以看到有捕获接口了

Wireshark学习篇(1)---NPF driver is not running error

在互联网这个行业里&#xff0c;怎能不熟悉几款抓包工具呢。将Wireshark作为首选学习工具&#xff0c;此工具功能比较强大&#xff0c;是基于网卡级别进行的抓包。在电脑上安装了 Wireshark后&#xff0c;首次登录的时候&#xff0c;会有"NPF driver is not running"…

[安装wireshark时,报“Error opening file for writing npf.sys”]

问题来源&#xff1a; 最近拷贝了别人一个win10的虚拟机&#xff0c;用wireshark抓帧时&#xff0c;一直找不到wifi的网卡&#xff0c;导致抓不了帧。 查找问题&#xff1a; 在win10下输入命令&#xff1a; C:\Program Files\Wireshark>tshark -DThe NPF driver isnt run…