gpt4 book ai didi

c++ - 在 C++ 中使用 tolower() 函数将字符串转换为小写字符

转载 作者:行者123 更新时间:2023-11-30 00:51:56 26 4
gpt4 key购买 nike

我有一个名为aisha的文本文件

This is a new file I did it for mediu.
Its about Removing stopwords fRom the file
and apply casefolding to it
I Tried doing that many Times
and finally now I could do

然后我编写了一个代码来读取该文本文件并将其保存到一个数组中,然后将一些字符转换为小写字母但我想要的是让代码将文件读取为字符串而不是 char

char myArray[200];

成为

`string myArray[200];`

我想我可以使用函数 tolower() 和一个字符串 std输入我使用的长代码但我不知道如何将我的代码更改为使用该功能的代码

我的代码是

#include <iostream>
#include <string>
#include <fstream>
#include<ctype.h>
int main()
{
using namespace std;

ifstream file("aisha.txt");
if(file.is_open())
{
file >> std::noskipws;
char myArray[200];

for(int i = 0; i < 200; ++i)
{


cout<<"i";
if (myArray[i]=='A')
cout<<"a";
if (myArray[i]=='T')
cout<<"t";
if (myArray[i]=='R')
cout<<"r";
else
if (myArray[i]!='I' && myArray[i]!='T' && myArray[i]!='R'&& myArray[i]!='A')
cout<<myArray[i];
}
file.close();

}

system("PAUSE");
return 0;
}

我在这个网站上看到了那个解决方案但我无法将它应用到我的代码中

#include <boost/algorithm/string.hpp>    

std::string str = "wHatEver";
boost::to_lower(str);

Otherwise, you may use std::transform:

std::string str = "wHatEver";
std::transform(str.begin(), str.end(), str.begin(), ::tolower);

最佳答案

以下代码可以解决您的问题:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>
#include <algorithm>
#include <iterator>

using namespace std;

int main(int argc, char **argv) {
ifstream ifs("file");
ostringstream oss;
char c;
while(true) {
c = ifs.get();
if(ifs.good())
oss.put(c);
else
break;
}
string s = oss.str();
transform(s.begin(), s.end(), ostream_iterator<char>(cout), ::tolower);
return 0;
}

关于c++ - 在 C++ 中使用 tolower() 函数将字符串转换为小写字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20309887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com