如何使用空格将信息放入字符串中?(来自数组)
How to put information into a string using whitespace? (From an array)
困惑.. 我的老师让我把这些信息从数组中放入一个字符串中......以下是她给我的确切方向:
"使用空格的位置,使用 this 和适当的字符串函数将名字存储在 fname 中,将姓氏存储在 lname 中(两个字符串变量)中。"
isspace 函数为我找到了空格,我把它放在变量"y"中,正如您将在下面的代码中看到的那样。 但我不知道如何使用空格将从数组输入的字符放入两个单独的字符串变量中!
。任何帮助将不胜感激!
这是我的代码:
// Zachary Law Strings.cpp
#include <iostream>
using namespace std;
#include <string>
#include <iomanip>
int main()
{ int x, i,y;
char name[] = "Please enter your name: ";
char answer1 [80];
i=0;
y=0;
cout << name;
cin.getline(answer1, 79);
cout << endl;
x=strlen(answer1);
for (int i = 0; i < x; i++){
cout << answer1[i] << endl;
if (isspace(answer1[i]))
{y=y+1;}}
cout << endl << endl;
cout << setw(80) << answer1;
cout << y;
return 0;}
你可以看到这个代码...我已经编辑了你的代码...希望,你会得到这个... :)
#include <iostream>
using namespace std;
#include <string>
#include <string.h>
#include <iomanip>
int main()
{
int x, i,y,fg=0,z;
char name[100];
string answer;
i=0;
y=0;
cout<<"Enter Your First & Last Name:";
cout << endl;
cin.getline(name,100);
x=strlen(name);
for (int i = 0; i < x; i++){
if (!fg&&isspace(name[i]))
{
y=i;
fg=1;
}
else if(fg==1&&!isspace(name[i]))
{
z=i;
fg=2;
}
answer+=name[i];
}
string fname=answer.substr(0,y);
cout<<fname<<endl;
string lname=answer.substr(z,x-z);
cout<<lname<<endl;
return 0;
}
相关文章:
- 使用 jsoncpp 解析 json 数组字符串
- C++ 对象数组字符串输入在控制台上不起作用
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 无法区分 JSON 对象/数组/字符串
- 为什么数组(字符串类型)的大小是 24 字节,带有单个空格元素
- 如何使用数组字符串进行输入验证
- (C++) 打印字符数组字符串的单词时出现问题
- 使用Back_inserter从任何位置的输入字符中使用Back_inserter进行过滤数组字符串
- C 函数反转字符数组字符串
- 如何在 c++ 中创建数组(字符串 [] )的向量
- 如何返回数组字符串的指针
- char数组字符串混淆
- 如何从字符数组字符串中提取空格
- 是否可以将 Bitset<8> 的值复制到数组字符串而不转换它们?
- C++数组字符串函数
- 如何在函数中操作字符数组(字符串)的指针,该指针在C / C ++中作为参数传递
- visual将音频文件存储到数组/字符串流C++中
- c++在char数组(字符串)上将what替换为
- 将数组字符串转换为intger c++(初学者)
- 需要设置数组字符串长度,使其恰好为13