代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
接受含有大写和小写字母的字符串,返回这个字符串的全
作者:
Dezai.CN
/ 发布于
2011/7/8
/
766
若要得到小写字母,找出大写字母并将其改为小写字母。若要得到大写字母则相反。
<div> <div class="cnblogs_code"> <pre><div><span style="color: #000000">#include </span><span style="color: #000000"><</span><span style="color: #000000">iostream</span><span style="color: #000000">></span><span style="color: #000000">#include </span><span style="color: #000000"><</span><span style="color: #0000ff">string</span><span style="color: #000000">></span><span style="color: #000000"></span><span style="color: #0000ff">using</span><span style="color: #000000"> </span><span style="color: #0000ff">namespace</span><span style="color: #000000"> std;</span><span style="color: #0000ff">bool</span><span style="color: #000000"> tolower_or_toupper(</span><span style="color: #0000ff">string</span><span style="color: #000000"> </span><span style="color: #000000">&</span><span style="color: #000000">str, </span><span style="color: #0000ff">char</span><span style="color: #000000"> option){ </span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; </span><span style="color: #0000ff">if</span><span style="color: #000000"> (option </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">l</span><span style="color: #800000">'</span><span style="color: #000000"> </span><span style="color: #000000">&&</span><span style="color: #000000"> option </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">u</span><span style="color: #800000">'</span><span style="color: #000000">) //使用字符'l'和'u'判断是转换为小写还是大写字母 { cout </span><span style="color: #000000"><<</span><span style="color: #000000"> option </span><span style="color: #000000"><<</span><span style="color: #000000"> endl; cout </span><span style="color: #000000"><<</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">The option is error! It must be 'l' or 'u'</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000"><<</span><span style="color: #000000"> endl; </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">false</span><span style="color: #000000">; } </span><span style="color: #0000ff">for</span><span style="color: #000000"> (i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">; i</span><span style="color: #000000"><</span><span style="color: #000000">str.length();i</span><span style="color: #000000">++</span><span style="color: #000000">) { </span><span style="color: #0000ff">if</span><span style="color: #000000"> (isupper(str.at(i)) </span><span style="color: #000000">&&</span><span style="color: #000000"> option </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">l</span><span style="color: #800000">'</span><span style="color: #000000">)//转换为小写的 { str.at(i) </span><span style="color: #000000">=</span><span style="color: #000000"> str.at(i) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span><span style="color: #000000">-</span><span style="color: #800000">'</span><span style="color: #800000">A</span><span style="color: #800000">'</span><span style="color: #000000">;//ascii码记不住没关系,临时转换就是了 } </span><span style="color: #0000ff">else</span><span style="color: #000000"> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (islower(str.at(i)) </span><span style="color: #000000">&&</span><span style="color: #000000"> option </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">u</span><span style="color: #800000">'</span><span style="color: #000000">)//转换为大写的 { str.at(i) </span><span style="color: #000000">=</span><span style="color: #000000"> str.at(i) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">A</span><span style="color: #800000">'</span><span style="color: #000000">-</span><span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span><span style="color: #000000">; } } </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">;}</span></div></pre> </div> 下面是一个测试程序: <div class="cnblogs_code"> <pre><div><span style="color: #000000">#include </span><span style="color: #000000"><</span><span style="color: #000000">iostream</span><span style="color: #000000">></span><span style="color: #000000">#include </span><span style="color: #000000"><</span><span style="color: #0000ff">string</span><span style="color: #000000">></span><span style="color: #000000"></span><span style="color: #0000ff">using</span><span style="color: #000000"> </span><span style="color: #0000ff">namespace</span><span style="color: #000000"> std;</span><span style="color: #0000ff">bool</span><span style="color: #000000"> tolower_or_toupper(</span><span style="color: #0000ff">string</span><span style="color: #000000"> </span><span style="color: #000000">&</span><span style="color: #000000">str, </span><span style="color: #0000ff">char</span><span style="color: #000000"> option);</span><span style="color: #0000ff">int</span><span style="color: #000000"> main(){ </span><span style="color: #0000ff">string</span><span style="color: #000000"> str </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">aFkFHkKFNDJHDNFkfksdjjek</span><span style="color: #800000">"</span><span style="color: #000000">; tolower_or_toupper(str, </span><span style="color: #800000">'</span><span style="color: #800000">l</span><span style="color: #800000">'</span><span style="color: #000000">);//全部转换为小写的 cout </span><span style="color: #000000"><<</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">The loer letters is : </span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000"><<</span><span style="color: #000000"> str </span><span style="color: #000000"><<</span><span style="color: #000000"> endl; tolower_or_toupper(str, </span><span style="color: #800000">'</span><span style="color: #800000">u</span><span style="color: #800000">'</span><span style="color: #000000">);//全部转换为大写的 cout </span><span style="color: #000000"><<</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">The upper letters is : </span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000"><<</span><span style="color: #000000"> str </span><span style="color: #000000"><<</span><span style="color: #000000"> endl; </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #800080">1</span><span style="color: #000000">;}</span></div></pre> </div> 下面的也是一个测试程序,只不过嫁了个文件操作: <div class="cnblogs_code"> <pre><div><span style="color: #000000">#include </span><span style="color: #000000"><</span><span style="color: #000000">iostream</span><span style="color: #000000">></span><span style="color: #000000">#include </span><span style="color: #000000"><</span><span style="color: #000000">fstream</span><span style="color: #000000">></span><span style="color: #000000">#include </span><span style="color: #000000"><</span><span style="color: #0000ff">string</span><span style="color: #000000">></span><span style="color: #000000"></span><span style="color: #0000ff">using</span><span style="color: #000000"> </span><span style="color: #0000ff">namespace</span><span style="color: #000000"> std;</span><span style="color: #0000ff">bool</span><span style="color: #000000"> tolower_or_toupper(</span><span style="color: #0000ff">string</span><span style="color: #000000"> </span><span style="color: #000000">&</span><span style="color: #000000">str, </span><span style="color: #0000ff">char</span><span style="color: #000000"> option);</span><span style="color: #0000ff">int</span><span style="color: #000000"> main(){ </span><span style="color: #0000ff">string</span><span style="color: #000000"> str </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">aFkFHkKFNDJHDNFkfksdjjek</span><span style="color: #800000">"</span><span style="color: #000000">; tolower_or_toupper(str, </span><span style="color: #800000">'</span><span style="color: #800000">l</span><span style="color: #800000">'</span><span style="color: #000000">); cout </span><span style="color: #000000"><<</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">The loer letters is : </span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000"><<</span><span style="color: #000000"> str </span><span style="color: #000000"><<</span><span style="color: #000000"> endl; str </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">aFkFHkKFNDJHDNFkfksdjjek</span><span style="color: #800000">"</span><span style="color: #000000">; tolower_or_toupper(str, </span><span style="color: #800000">'</span><span style="color: #800000">u</span><span style="color: #800000">'</span><span style="color: #000000">); cout </span><span style="color: #000000"><<</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">The upper letters is : </span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000"><<</span><span style="color: #000000"> str </span><span style="color: #000000"><<</span><span style="color: #000000"> endl; </span><span style="color: #008000">//</span><span style="color: #008000">测试一个文件的更改</span><span style="color: #008000"></span><span style="color: #000000"> </span><span style="color: #0000ff">char</span><span style="color: #000000"> buffer[</span><span style="color: #800080">256</span><span style="color: #000000">]; </span><span style="color: #0000ff">char</span><span style="color: #000000"> delim; fstream file; file.open(</span><span style="color: #800000">"</span><span style="color: #800000">tmp</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (file.is_open()) { file.</span><span style="color: #0000ff">get</span><span style="color: #000000">(buffer,</span><span style="color: #800080">256</span><span style="color: #000000">, delim</span><span style="color: #000000">=</span><span style="color: #000000">EOF); cout </span><span style="color: #000000"><<</span><span style="color: #000000"> buffer </span><span style="color: #000000"><<</span><span style="color: #000000"> endl; file.close(); } str.assign(buffer); tolower_or_toupper(str, </span><span style="color: #800000">'</span><span style="color: #800000">u</span><span style="color: #800000">'</span><span style="color: #000000">); cout </span><span style="color: #000000"><<</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">The upper letters is : </span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000"><<</span><span style="color: #000000"> str </span><span style="color: #000000"><<</span><span style="color: #000000"> endl; </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #800080">1</span><span style="color: #000000">;}</span></div></pre> </div> 编译和运行: <div class="cnblogs_code"> <pre><div><span style="color: #000000">[hanxi@hanxi-workstation Documents]$ g++ letter.cpp testletter.cpp -o strtest</span></div><div><span style="color: #000000">[hanxi@hanxi</span><span style="color: #000000">-</span><span style="color: #000000">workstation Documents]$ .</span><span style="color: #000000">/</span><span style="color: #000000">strtestThe loer letters </span><span style="color: #0000ff">is</span><span style="color: #000000"> : afkfhkkfndjhdnfkfksdjjekThe upper letters </span><span style="color: #0000ff">is</span><span style="color: #000000"> : AFKFHKKFNDJHDNFKFKSDJJEKWalter</span><span style="color: #800000">'</span><span style="color: #800000">s goal in life was to become a successful surgeon. First, though, he had to get through high school, so he concentrated all his efforts on his studies —— in particular, biology, chemistry, and math. Because he worked constantly on these subject</span><span style="color: #800000"></span><span style="color: #000000">The upper letters </span><span style="color: #0000ff">is</span><span style="color: #000000"> : WALTER</span><span style="color: #800000">'</span><span style="color: #800000">S GOAL IN LIFE WAS TO BECOME A SUCCESSFUL SURGEON. FIRST, THOUGH, HE HAD TO GET THROUGH HIGH SCHOOL, SO HE CONCENTRATED ALL HIS EFFORTS ON HIS STUDIES —— IN PARTICULAR, BIOLOGY, CHEMISTRY, AND MATH. BECAUSE HE WORKED CONSTANTLY ON THESE SUBJECT</span></div></pre> </div> </div>
试试其它关键字
字符串
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3