C++ 我正在编写一个使用数组将小写字母转换为大写字母的代码

C++ 我正在编写一个使用数组将小写字母转换为大写字母的代码,c++,C++,我有代码,但它打印的字母大写,但也打印一些奇怪的字符后。我只是想知道如何拿到这些信 执行图片的程序。 使用名称空间std int main() { const int SIZE = 81; // Constant for size of an array const int MIN_LOWERCASE = 97;

我有代码,但它打印的字母大写,但也打印一些奇怪的字符后。我只是想知道如何拿到这些信

执行图片的程序。

使用名称空间std

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;


}
intmain()
{
const int SIZE=81;//数组大小的常量
const int MIN_LOWERCASE=97;//ASCII中小写字母的开头
const int MAX_LOWERCASE=122;//ASCII中小写字母的结尾
字符行[SIZE];//初始化输入字符行

cout=MIN\u小写&&line[count]您需要使用您读取的文本的实际大小。否则您将打印额外字符

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

您可能需要
#include
才能使用
strlen()

您需要使用您读取的文本的实际大小。否则,您将打印额外的字符

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

<>你可能需要<代码>包含<代码> STRULN()/CUB> .< /P> <代码> CUT= MIN LeLeCase&Deal[Cuth] < Cord> CUT= Min LyrErrase&Leal[Curt],P> > C++中的习惯用法是

#include <string>
#include <locale>
#include <algorithm>
#include <iterator>
#include <iostream>

int main()
{
    std::locale loc(""); //< the current system locale

    std::string line;  //< will contain the input line
    std::cout << "Enter a string of 80 or fewer characters:\n";
    std::getline(std::cin,line);

    std::string lower; //< will contain the output

    // This is the "key" of everything
    std::transform(line.begin(),line.end(), // transform the entire input...
        std::back_inserter(lower), // by writing into the back of the output string ...
        [&loc](auto c){ return std::tolower(c,loc); }); // the result of std::tolower applied to all character, using the system locale

    std::cout << "The transformed string is:\n" << lower << std::endl;

    return 0;
}

// look ma! No pointers, array sizes, overflows and explicit memory management.
// And works consistently with the language your computer is set up.
#包括
#包括
#包括
#包括
#包括
int main()
{
std::locale loc(“”;//<当前系统语言环境
std::string line;//<将包含输入行

STD::CUT> P> C++中的习惯用法是

#include <string>
#include <locale>
#include <algorithm>
#include <iterator>
#include <iostream>

int main()
{
    std::locale loc(""); //< the current system locale

    std::string line;  //< will contain the input line
    std::cout << "Enter a string of 80 or fewer characters:\n";
    std::getline(std::cin,line);

    std::string lower; //< will contain the output

    // This is the "key" of everything
    std::transform(line.begin(),line.end(), // transform the entire input...
        std::back_inserter(lower), // by writing into the back of the output string ...
        [&loc](auto c){ return std::tolower(c,loc); }); // the result of std::tolower applied to all character, using the system locale

    std::cout << "The transformed string is:\n" << lower << std::endl;

    return 0;
}

// look ma! No pointers, array sizes, overflows and explicit memory management.
// And works consistently with the language your computer is set up.
#包括
#包括
#包括
#包括
#包括
int main()
{
std::locale loc(“”;//<当前系统语言环境
std::string line;//<将包含输入行

std::cout事实上,char[]的getline/cin/scanf等函数解释如下:

  • 字符串初始化为
    char c[10];
    。输入为“abcd”
  • 首先,c[i]初始化为未知值,因为它是局部变量(如果它是全局变量,可以假设c[i]=0)

  • 第二,如果您输入,c[i]的值仅在
    0的地方更改,实际上是getline/cin/scanf等。下面的示例解释了char[]的函数:

    • 字符串初始化为
      char c[10];
      。输入为“abcd”
    • 首先,c[i]初始化为未知值,因为它是局部变量(如果它是全局变量,可以假设c[i]=0)

    • 第二,如果您输入,c[i]的值仅在
      0的位置更改fist
      line[count]-32
      无效!fist
      line[count]-32
      没有效果!当然,在使用固定阵列之前可以学习这种方式,因为这种方式更接近需求中的概念。当然,在使用固定阵列之前可以学习这种方式,因为这种方式更接近需求中的概念。