c++ - 将用户输入的字符串大写

标签 c++ capitalization

我正在解决我遇到的问题:

the user input the number of names in a list and then input the names. Then I have to change the strings so that every name has the first character capitalized and the rest should be low letters.

如果名称是这样输入的:

2 EliZAbetH paUL

输出应该是这样的:

Elizabeth Paul

这是我试过的:

#include <iostream>
#include<string>
#include <cctype>
#include <stdio.h>
#include <ctype.h>

using namespace std;
const int FYLKJASTAERD = 100;
int main()
{    
    int FjoldiOrd = 0;

    //get the number of students
    cout << "Enter the number of students: ";
    cin >> FjoldiOrd;

    string Nofn[FYLKJASTAERD];

    //get student names
    for (int i=0; i<FjoldiOrd; i++)
    {
         cin >> Nofn[i];

         if (Nofn.length()>0)
         {
             Nofn[0] = std::toupper(Nofn[0]);
             for (size_t i = 1; i < Nofn.length(); i++)
             {
                  Nofn[i] = std::tolower(Nofn[i]);
             }
         }
    }
    cout << Nofn[i] << endl;

    return 0;
}

程序没有编译,它给了我这个错误:

request for member lenght in Nofn which is of non-class type 'std::string[100]

问题:

请问我做错了什么?

注意事项:

我用输入名称完成了一个更简单的版本,效果很好。

    string name;
    cout << "Please enter your first name: ";
    cin >> name;

    if( !name.empty() )
    {
        name[0] = std::toupper( name[0] );

        for( std::size_t i = 1 ; i < name.length() ; ++i )
            name[i] = std::tolower( name[i] );
    }
    cout << name << endl;

    return 0;
}

最佳答案

代码:

    string Nofn[FYLKJASTAERD];

是一个字符串数组。所以你需要添加一个 [index] 来获取字符串。

因此你不能要求 Nofn 的长度。

你可以这样做

Nofn[i].length();

获取字符串数组中第i个位置的字符串长度。

此外,您还使用了变量名称 i 两次。这很糟糕。

也许这会更好(但我自己还没有尝试过):

//get student names
for (int i=0; i<FjoldiOrd; i++)
{
     cin >> Nofn[i];

     if (Nofn[i].length()>0)
     {
         Nofn[i][0] = std::toupper(Nofn[i][0]);
         for (size_t j = 1; j < Nofn[i].length(); j++)
         {
              Nofn[i][j] = std::tolower(Nofn[i][j]);
         }
     }

}
cout << Nofn[i] << endl;

但是……

我不认为您的程序完全符合您的要求。 cin 到一个字符串将在一个空格处停止,因此您将在树干中获得名称 - 而不仅仅是一个带有名称的字符串。要获取包含空格的名称,请像这样使用 getline:

string name;
getline(cin, name);

这会使您的大写/小写代码复杂化,因为您必须检测空格。也许这可以做到:

#include <iostream>
#include<string>
#include <cctype>
#include <stdio.h>
#include <ctype.h>

using namespace std;

void fixString(string& s)
{
    bool doUpperCase = true;

    for (int i=0; i < s.length(); i++)
    {
        if (s[i] == ' ')
        {
            doUpperCase = true;
        }
        else
        {
             if (doUpperCase)
             {
                 s[i] = std::toupper(s[i]);
                 doUpperCase = false;
             }
             else
             {
                 s[i] = std::tolower(s[i]);
             }
         }
    }
}

const int FYLKJASTAERD = 100;
int main()
{    
    int FjoldiOrd = 0;

    //get the number of students
    cout << "Enter the number of students: ";
    cin >> FjoldiOrd;
    while (cin.get() != '\n')
    {
        continue;
    }

    string Nofn[FYLKJASTAERD];

    //get student names
    for (int i=0; i<FjoldiOrd; i++)
    {
         getline(cin, Nofn[i]);
         fixString(Nofn[i]);
         cout << Nofn[i] << endl;
    }

    return 0;
}

关于c++ - 将用户输入的字符串大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32789995/

相关文章:

c++ - boost::interprocess message_queue - Windows 7 低完整性进程

c++ - 带符号的 Char ATAN2 和 ATAN 近似值

java - Hibernate属性名称大写问题

javascript - 无论大写,检查对象中的匹配键

c# - 为什么在 Java(和其他)中大写类名只是一个建议而不是规则?

c++ - XCode 5 不会破坏 C++ 模板

c++ - 将循环方法更改为另一种方法

.net - Visual Studio CLR 项目

iOS TextField 自动大写不起作用

c# - 使用C#将字符串中的单词大写