操作字符串c++
Manipulating strings c++
我正在为类做这项作业,遇到了一个问题。
该程序的目标是让用户使用
- 输入他们的名字
- 然后程序会要求输入昵称
-
最后,程序将输出您的
- 名字
- 昵称引号中的所有大写字母
- 姓
更详细的指令版本可以在这里找到
因此,我的问题在于如何操作字符串以将昵称放在第一个和最后一个之间。
我使用cin.getline()
在开头输入名字和姓氏我似乎在字符串中找不到姓氏。有没有一种方法可以将字符串/char[]读取到数组中某个位置之后的另一个字符串中。
就像某个c++原生库中有一个函数(伪代码)
readFromPoint(array, otherArray, beginPoint, stopPoint);
还是我只需要建立自己的循环。
我想把我的名字和姓氏输入到一个字符串中。然后把它们分成两条单独的绳子。然后我将(first,昵称,last)放入第一个也是原始字符串中。
我知道如果我只使用两个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 << "nnEnter 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] != ' '); // 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){
}
当然,如果我想打开这个,我不能使用任何外部库。只有那些C++原生库。
看看这个程序,它使用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;
}
输出为:-
Ankit Acharya
Programmer
Ankit Programmer Acharya
希望这能很好地解决你的问题!!
如果您使用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;
}
这是一个全名为"你好世界",昵称为"昵称"的表意符号。
您没有用cin.getline()
获取姓氏的原因是因为它只获取它看到的第一个字符串,在空格之前。重复cin.getline()
两次(或使用以下代码)以获得名字和姓氏:
std::string firstName, lastName;
std::cin >> firstName >> lastName;
这只使用<iostream>
和<cstring>
中包含的本机c++内容。如果您需要将昵称转换为大写的帮助,请告诉我。
编辑:除非迫不得已,否则永远不要使用c风格的字符串;<string>
中包含了您需要的所有工具。