如何将文件中的所有内容都转换为小写? [英] how to convert all in file to lowercase?

查看:146
本文介绍了如何将文件中的所有内容都转换为小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开始程序时遇到麻烦.我需要从文件中读取每个单词,然后将其转换为小写.找到每个单词后,我想std::cout.我认为我需要以某种方式使用c_str().我猜我应该使用类似的东西:

I am having trouble getting started with a program. I need to read in each word from a file, then convert it to lower case. I would like to std::cout each word after I find it. I assume I need to use c_str() some how. I am guessing I should use something like:

ofs.open(infile.c_str());    

但是如何小写?

string[i] = tolower(string[i]);

推荐答案

您可以使用std::tolower()函数="nofollow noreferrer">语言环境.不确定这是否是您要寻找的东西,但这是快速解决您的问题的方法(据我了解).

You can use the std::tolower() function from locale. Not sure if this is what you are looking for, but here is a quick solution to your problem (as i understand it).

#include<iostream>
#include<string>
#include<fstream>
#include<locale>

int main(){
  std::string input;
  std::ifstream inputStream;
  inputStream.open("input.txt", std::ifstream::in);


  while(inputStream >> input){
    for(auto s : input)
    {
      std::cout << std::tolower(s, std::locale());
    }
    std::cout << " ";
  }

  return 0;
}

这篇关于如何将文件中的所有内容都转换为小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆