与上下案例相关的C ++帮助

时间:2015-10-05 19:54:08

标签: c++

我希望代码能够在句子中搜索一个单词并查看它的第一个字母是否为小写。

如果是,那就比它大写吧。例如: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;
}

1 个答案:

答案 0 :(得分:1)

s重新定义为std::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
相关问题