c++ - 我正在编写代码以使用数组将小写字母转换为大写字母

标签 c++

我有代码,但它以大写形式打印字母,但之后还会打印一些奇怪的字符。我只是想知道如何获取这些字母。

程序执行图片。 enter image description here

使用命名空间标准;

int main()
{

const int SIZE = 81;                                                       // Constant for size of an array
const int MIN_LOWERCASE = 97;                                              // Start of lowercase letters in ASCII
const int MAX_LOWERCASE = 122;                                             // End of lowercase letters in ASCII

char line[SIZE];                                                           // Initializing character line for input

cout << "Enter a string of 80 or fewer characters:\n";
cin.getline(line,SIZE);                                                    // Getting input from the user.

for (int count = 0; count < SIZE; count++)
{
    if (line[count] >= MIN_LOWERCASE && line[count] <= MAX_LOWERCASE)     // Checking whether the selected letter is in the reange of lowercase letters.
    {
         line[count] - 32;                                                
         cout << static_cast<char>(line[count] - 32);                     // converting and displaying lowercase letters to uppercase letters.

    }
    else
    {
        cout << static_cast<char>(line[count]);//Displaying the same character if it is in uppercase.                 

    }

}
cout << endl;


system("pause");
return 0;


}

最佳答案

您需要使用您阅读的文本的实际大小。否则你会打印额外的字符。

for (int count = 0; count < strlen(line); count++)

您可能需要 #include <cstring>使用 strlen() .

关于c++ - 我正在编写代码以使用数组将小写字母转换为大写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40904261/

相关文章:

c++ - 给定抽象基类 X,如何创建另一个模板类 D<T>,其中 T 是从 X 派生的类的类型?

java - 如何连接到 java 中的 Binder C++ 服务?

c++/g++ 比较 WORD 和 CHAR 的魔数(Magic Number)

c++ - map 模板类

c++ - Boost::DateTime 无法正确解析

c# - 从 C# 函数在 C++ COM dll 中传递字符串

c++函数的返回类型是其输入的函数

c++ - 线程等待如何影响程序的执行时间?

C++重载优先于特化?

c++ - 如何获取存储在 C++ 列表中的类对象?