有关大小写的c++帮助

C++ help related to Upper and Lower cases

本文关键字:c++ 帮助 大小写      更新时间:2023-10-16

我想让代码搜索句子中间的单词,看看它的第一个字母是否小写。

如果是,则将其变为大写。例如:John讨厌每天使用c++,这会把c++中的c改为大写。

代码

#include <iostream>
#include <string>
#include<cstdlib>
#include<fstream>
using namespace std;
ifstream in_stream;
ofstream out_stream;
int main()
{
    in_stream.open("in.dat");
    out_stream.open("out.dat");
    char s[256];
    in_stream>>s;
    s[0] = tolower(s[0]);
    out_stream<<s;
    in_stream.close();
    out_stream.close();
    system("Pause");
    return 0;
}

重新定义sstd::string类型

std::string wordOfInterest = "c++" // Change as per your needs
std::string::size_type pos = s.find(wordOfInterest); // Index at which the word of interest starts
if (pos != std::string::npos) s[pos] = toupper(s[pos]); // Updating the value at index with its upper case counterpart