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

convert strings to lowercase characters using tolower() function in c++

本文关键字:转换 字符串 字符 函数 中的 c++ tolower 使用      更新时间:2023-10-16

我有一个名为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

希望这能帮助你。