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

时间:2013-12-01 07:40:35

标签: c++ arrays text lowercase

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

4 个答案:

答案 0 :(得分:2)

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

#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 :(得分:2)

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;
  }

答案 2 :(得分:1)

有多种方法可以做到这一点。最后你想要一个容器,容器是输入文件数据的所有小写字符串,它是相当简单的。这里使用的算法的前提是:

  • 使用std::istream_iterator<std::string>对从输入文件加载字符串向量。
  • 使用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

希望能帮到你。

答案 3 :(得分:1)

请在此处查看解决方案: std::tolower and Visual Studio 2013

例如,你可以这样做:

std::transform(test.begin(), test.end(), test.begin(),
               [](unsigned char c) { return std::tolower(c); });