我想在新行打印阵列的每个插槽

i want to print every slot of my array at a new line

本文关键字:插槽 阵列 打印 新行      更新时间:2023-10-16

这是我当前的代码,我必须在新行上打印我名字的每个世界

include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main ()
{
    string a;
    char j[100];
    int i, c, b; 
    cout <<"enter your full  name ";
        getline(cin,a);
        cout << " ur name is " << a << endl;
c=a.size(); 
for (b=0; b<=c; b++)
{
j[b]=a[b];
j[b]='';
}
system ("pause");
return 0; 
}

如何在新行上打印我名字的每个部分? 例如:输入:杰罗格·阿什利·马克。 输出:乔治(换行符) 阿什利(换行)马克

这是一个

有点复杂的方法,我更喜欢评论中显示的方法。但是,如果您想避免字符串流,这是实现您正在寻找的另一种方法。它还将支持逗号分隔的名称。

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main()
{
    string a;
    char j[100];
    int i, c, b;
    cout << "enter your full  name ";
    getline(cin, a);
    cout << " ur name is " << a << endl;
    c = a.size();
    bool space = false;
    for (auto iter = a.begin(); iter != a.end(); iter++)
    {
        if (isalpha(*iter) == false)
        {
            if (space == false)
            {
                cout << std::endl;
                space = true;
            }
        }
        else
        {
            cout << (*iter);
            space = false;
        }
    }
    system("pause");
    return 0;
}