将字符串数组中的单个字符大写
Capitalizing individual characters in string array
我必须在字符串数组中大写名字和姓氏,但是我被困在如何在我将字符串值输入到数组后访问特定值。
下面是我的两个函数,第一个函数将值读入两个单独的数组,第二个函数应该将名字和姓氏大写,同时将所有其他字符更改为小写。
void read(string names[],int DVDrent[],int& n)
{
n=0; // # of data calculated
cin >> names[n] >> DVDrent[n]
while(cin)
{
n++;
cin >> names[n] >> DVDrent[n];
}
}
void reformat(string& names)
{
int nlen;
nlen = names.length();
names[0] = toupper(names[0]);
for (int i=1; i<nlen; i++)
{
if (names[i] == ',')
names[i+1] = toupper(names[i+1]);
names[i+2] = tolower(names[i+2]);
}
}
如果我只是将数据存储为字符串,则第二个函数可以工作。我现在卡住了,因为我不确定如何读取数组的特定字符。
作为参考,我输入的数据如下:
./a.out < data > output
数据:
smith,EMILY 3
Johnson,Daniel 2
williAMS,HanNAH 0
joneS,Jacob 4
bROwn,MicHAEL 5
DAVIS,ETHAn 2
millER,soPhiA 0
TAYlor,matthew 1
andERSON,aNNa 7
期望输出:
Smith,Emily 3
Johnson,Daniel 2
William,Hannah 0
.
.
.
Anderson,Anna 7
etc.
为您的reformat()
方法试试:
void reformat(string& names) {
bool isUpper = true; // store if the next letter should be upper
for(size_t i = 0; i < names.length(); ++i) {
if (names[i] == ',') {
isUpper = true; // if it's a comma, make the next letter upper
}
else if (isUpper) {
names[i] = toupper(names[i]);
isUpper = false; // make the next letter lower
}
else {
names[i] = tolower(names[i]);
}
}
}
相关文章:
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 如何读取单个字符并在输入两个字符序列时输出? 使用 while 循环和C++
- 为什么char不是单个字符
- 使用单个变量对字符输入进行计数
- 将字符向量转换为单个字符串
- C++ 字符串单个字符
- C++ - 使用结构数据类型将单词中的单个小写字符更改为大写,反之亦然
- 将单个字符转换为 std::string 前缀 \x01
- 将单个字符串/字符输入串行监视器
- 尝试一次将单个字符读取到大小不确定的数组中
- C++ 如何将字符串拆分为单个字符
- 如何在C++中将单个字符哈希为相对较小的素数?
- 如何将像'A'这样的单个字符替换为类似"10"?
- 我通过迭代加法将二进制数转换为十进制并检查单个字符(请参阅代码)的方法有什么问题?
- std::bad_alloc 将文本文件中的单个字符存储到矢量C++中时
- 查找单个字符串中重复次数最多和次数第二多的字符
- 如何从用户那里获取单个字符,直到他输入"."或输入
- 字符串中单个字符作为数组的数据类型是什么
- 字符串中的重音,如何在单个字符中存储重音字符
- 从字符串中提取单个字符并将其转换为 int