使用c++中的tolower()函数将字符串转换为小写字符
convert strings to lowercase characters using tolower() function in c++
我有一个名为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 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;
}
1)我希望这个例子能帮助你
#include "stdafx.h"
#include "iostream"
#include <stdio.h>
#include <ctype.h>
using namespace std;
int main ()
{
int i=0;
char str[30]="TESt STRING.n";
char c;
while (str[i])
{
c=str[i];
str[i]= (tolower(c));
cout<<str[i];
i++;
}
return 0;
}
请参阅此处的解决方案:std::tolower和Visual Studio 2013
例如,你可以做:
std::transform(test.begin(), test.end(), test.begin(),
[](unsigned char c) { return std::tolower(c); });
有很多方法可以做到这一点。最后,您想要一个容器来容纳输入文件数据的所有小写字符串,这是非常直接的。这里使用的算法的前提是:
- 使用CCD_ 2对从输入文件加载字符串向量
- 使用
std::for_each
算法枚举向量中的每个字符串,执行自定义操作 - 我们正在执行的自定义操作是枚举当前字符串中的每个字符,将该字符转换为小写等效字符
最后,代码看起来像这样:
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <string>
#include <cctype>
// transform a string in-place to lower case
struct StrLower
{
void operator()(std::string& s) const
{
for (std::string::iterator it = s.begin(); it != s.end(); ++it)
*it = static_cast<char>(std::tolower(static_cast<unsigned char>(*it)));
}
};
int main(int argc, char *argv[])
{
// build a vector of std::string from the input file
std::ifstream inf("aisha.txt");
std::vector<std::string> data (
(std::istream_iterator<std::string>(inf)),
std::istream_iterator<std::string>());
// for each string in the vector, transform to lower case.
for_each(data.begin(), data.end(), StrLower());
// display all the transformed strings
std::copy(data.begin(), data.end(),
std::ostream_iterator<std::string>(std::cout, "n"));
return 0;
}
输入(aisha.txt)
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
输出
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
希望这能帮助你。
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数