将用户输入的字符串大写

Capitalizing Strings Input from User

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

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 到一个字符串将在 space 处停止,因此您将在主干中获得名称 - 而不仅仅是一个带有名称的字符串。要获取包含 spaces 的名称,请像这样使用 getline:

string name;
getline(cin, name);

这会使您的 upper/lower 案例代码复杂化,因为您必须检测 space。也许这可以做到:

#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;
}