如何遍历字符串并将其转换为小写?

编程入门 行业动态 更新时间:2024-10-25 14:24:20
本文介绍了如何遍历字符串并将其转换为小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道不久前还有其他几个问题,但是我仔细检查了一下,仍然不明白.我正在尝试以字符串形式获取用户输入,然后遍历该字符串,将所有大写字母都转换为小写字母,以便可以将所有字母全部显示为小写字母.我要去哪里错了?

I know there were a couple of other questions on this from a while back, but I looked over those and still couldn't figure it out. I'm trying to take user input in the form of a string, then loop through that string converting all of the uppercase to lowercase so that I can display it all in lowercase. Where am I going wrong?

int main() { cout << "Enter Text: "; string Text; getline(cin, Text); for(int i=0; i<Text.length(); i++) { if(islower(Text[i]) == false) { tolower(Text[i]); i++; } Text[i] = Text[i]; } cout << "Your text is: "; cout << Text; cout << "\n"; }

我对C ++还是很陌生,如果我说即使在哪里出了错我也有很多想法,我会撒谎.第11行,其中 for 循环表示正在尝试比较两个不同的符号,但是我不知道这意味着什么,或者这是否是我的问题的根源.第15行的 tolower()表示正在忽略使用纯属性声明的函数的返回值",但我仍然不知道这是什么意思.请帮忙.

I'm very new to C++ and I'd be lying if I said I had much of an idea even where I was going wrong. Line 11, where the for loop is says that it's trying to compare two different signs, but I don't know what that means or if that's the source of my problem. Line 15 where the tolower() is says that it's 'ignoring return value of function declared with pure attribute' but I still don't know what that means either. Please help.

推荐答案

几点:

  • tolower 返回小写字符(如果存在)('A'变为'a','a'不变,'9'不变,等等)
  • Text [i] = Text [i]; 行不执行任何操作,您需要 Text [i]= tolower(Text [i]);
  • 无需检查每个字符是否为小写, tolower 会为您处理
  • tolower returns the lowercase character if it exists ('A' becomes 'a', 'a' is unchanged, '9' is unchanged, etc.)
  • The line Text[i] = Text[i]; does not do anything, you want Text[i] = tolower(Text[i]);
  • There is no need to check if each character is lowercase, tolower will handle that for you

简体:

#include <iostream> using namespace std; int main() { cout << "Enter Text: "; string Text; getline(cin, Text); for (int i = 0; i < Text.length(); i++) Text[i] = tolower(Text[i]); cout << "Your text is: "; cout << Text; cout << "\n"; }

更多推荐

如何遍历字符串并将其转换为小写?

本文发布于:2023-11-10 03:10:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   字符串   转换为   并将其

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!