C++ 操纵字符串c++;

C++ 操纵字符串c++;,c++,arrays,string,C++,Arrays,String,我正在做这个课堂作业,遇到了一个问题 该计划的目标是让用户 输入他们的名字,然后是姓氏 然后程序会要求输入昵称 最后,程序将输出您的 名字 将所有大写字母用引号括起来 姓 可以找到更详细的说明版本 因此,我的问题在于如何操作字符串以将昵称放置在第一个和最后一个之间 我正在使用cin.getline()在开头输入名字和姓氏。我似乎在字符串中找不到姓氏。是否有方法将字符串/char[]读入数组中某个位置后的另一个字符串中 如果在某个C++原生库中有一个函数,它就变成了(伪代码)< /强> 或

我正在做这个课堂作业,遇到了一个问题

该计划的目标是让用户

  • 输入他们的名字,然后是姓氏
  • 然后程序会要求输入昵称
  • 最后,程序将输出您的

  • 名字
  • 将所有大写字母用引号括起来
可以找到更详细的说明版本

因此,我的问题在于如何操作字符串以将昵称放置在第一个和最后一个之间

我正在使用
cin.getline()
在开头输入名字和姓氏。我似乎在字符串中找不到姓氏。是否有方法将字符串/char[]读入数组中某个位置后的另一个字符串中

如果在某个C++原生库中有一个函数,它就变成了<强>(伪代码)< /强>

或者我只是需要建立自己的循环

我想在一个字符串中输入我的名字和姓氏。然后将它们放在两个单独的字符串中。然后我将(第一个,昵称,最后一个)放入第一个和原始字符串中

我知道如果我只使用两个cin语句,它可以帮我省去很多心痛。(我相信我听过别人说这很危险)

这是我的密码:

#include <iostream>
#include <cstring>
using namespace std;

char Name[46], // the full names leangth - 2
     firstName[16],
     lastName[16],
     nickName[16];
     int numOSpaces;

//void rearrangeName();
int FindSpace(string, int);
int FindNull(string);
int main(){

    cout << "Pleast enter your first then last name...\n";
    cin.getline(Name, 30);
    int ender =  FindSpace(Name, strlen(Name));

    // here we check if you entered the right amount of spaces
    while (numOSpaces > 1 || numOSpaces < 1){
        if (numOSpaces > 1)
        cout << "There are too many spaces only put one space:\n";
        if (numOSpaces < 1)
        cout << "Please separate your first and last name:\n";
        cin.getline(Name, 30);
        ender =  FindSpace(Name, strlen(Name));
    }

    cout << "Hello " << Name <<".\n";

    cout << "\n\nEnter your nickname...\n";\

    cin.getline(nickName, 30);
    cout << "Whats up " <<nickName << "!" << endl << endl;

    // Now we separate the first name and the last name 
    //rearrangeName();
}

// used to tell where user stopped inputting into the string
int FindNull(string find){
    int index = 0;
    do{
        index++;
    }while (isprint(find[index])); // this stops at the null terminator
//      }while (find[index] != '\0');  // as will this 
    return index;
}   

// here we find the first space 
int FindSpace(string mystring, int size){
    int Space;
    int spaceCount = 0;
    for (int index; index < size; index++){
        if (isspace(mystring[index])){
            Space = index;
            spaceCount++;
        }
    }
    numOSpaces = spaceCount;
    return Space;
}

void findName(string fullName, string otherName){

}
#包括
#包括
使用名称空间std;
char Name[46],//全名leangth-2
名字[16],
姓氏[16],
昵称[16];
内核空间;
//void name();
int FindSpace(字符串,int);
int FindNull(字符串);
int main(){
cout 1 | |天体空间<1){
如果(numospace>1)

cout无法使用
cin.getline()
获取姓氏的原因是,它只获取它看到的第一个字符串,在空格之前。重复
cin.getline()
两次(或使用以下代码)以获取名字和姓氏:

std::string firstName, lastName;
std::cin >> firstName >> lastName;

这只使用了原生C++的东西,包含在<代码> <代码>和<代码> <代码>中。 如果您需要帮助将昵称转换为大写,请告诉我


编辑:除非迫不得已,否则不要使用c样式字符串;您需要的所有工具都包含在

中。如果您使用
std::string
,您将有一个轻松得多的时间

#include <iostream>
#include <string>

void capitalize(std::string& str) {
  for(size_t i = 0; i < str.length(); ++i) {
    str[i] = toupper(str[i]);
  }
}

int main() {
  std::string first_name, last_name, nickname;
  std::cout << "Enter full name: " << std::endl;
  std::cin >> first_name >> last_name;
  std::cout << "Enter nickname: " << std::endl;
  std::cin >> nickname;

  capitalize(nickname);

  std::cout << first_name << " \"" << nickname << "\" " << last_name << std::endl;
}
#包括
#包括
void大写(std::string和str){
对于(大小i=0;i>last_name;
std::cout昵称;
大写(昵称);

std::cout看看这个程序,它使用
std::string
很好地完成了你的工作,因为它有一些奇特的功能,比如
find
insert
,等等:-

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string name, nickname;
    getline(cin,name);
    getline(cin,nickname);
    nickname.insert(0," ");     // insert a space before nickname
    int pos = name.find(" ");   // find where space is becuase space separates first & last name
    name.insert(pos, nickname);     // insert nickname at the space, it doesn't trouble because we have a space before nickname
    cout << name;
    return 0;
}

<希望这能很好地解决你的问题!!< /p>使用<代码> STD::String < /C> >,这是C++标准库的一部分,然后一切都变得容易了。在C++中使用原始字符数组是个可怕的想法。<代码>字符名称〔46〕
等。既然您已经在函数参数中使用了
std::string
,为什么不也使用
std::string
?这不是
getline
所做的。
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string name, nickname;
    getline(cin,name);
    getline(cin,nickname);
    nickname.insert(0," ");     // insert a space before nickname
    int pos = name.find(" ");   // find where space is becuase space separates first & last name
    name.insert(pos, nickname);     // insert nickname at the space, it doesn't trouble because we have a space before nickname
    cout << name;
    return 0;
}
Ankit Acharya
Programmer
Ankit Programmer Acharya