0

getline(cin, input);変数の入力に使用して入力文字列を分割するにはどうすればよいですか? 次のように char 配列に分割したい:

char[] = { // What goes here to make it read the input variable and split it into chars }

これを行う方法はありますか?

そして、皆さんの新しい投稿はどうですか?どれが一番いいですか?入力変数を読み取り、その文字を char 配列に格納するために変更する必要があるシーザー暗号コードを次に示します。

// Test Code ONLY
// Not A Commercial Program OR A Crypter
// THIS IS A TEXT CIPHERER

#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <vector>

using namespace std;

int main()

{
    string in;
    string out;
    char lower[25] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
    char upper[25] = {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z};
    char upcip[25] = {Z,Y,X,W,V,U,T,S,R,Q,P,O,N,M,L,K,J,I,H,G,F,E,D,C,B,A};
    char locip[25] = {z,y,x,w,v,u,t,s,r,q,p,o,n,m,l,k,j,i,h,g,f,e,d,c,b,a};
    cout << "Enter PlainText: ";
    getline(cin, in);
    // which sample goes here to read the input var char by char, then store the chars in order in a char array in your opinion?
    return 0;
}
4

2 に答える 2

4

文字列すでに文字の配列です:

for (std::string line; std::getline(std::cin, line); )
{
    for (std::size_t i = 0, e = line.size(); i != e; ++i)
    {
        std::cout << "char[" << i << "] = '" << line[i] << "'\n";
    }
}
于 2012-09-13T12:28:43.867 に答える
0
string str;
    std::getline(cin,str);
    char* pArr = new char[str.size() + 1]; // add 1 for zero element which is the end of string
    strcpy(pArr,str.c_str());

/*
some actions with pArr, for ex.
while(*pArr)
    std::cout << *pArr; // output string on the screen

*/

// updated: release memory obtained from heap
delete [] pArr;
于 2012-09-13T12:32:21.937 に答える