Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;从字符到字符串的转换_C++ - Fatal编程技术网

C++ C++;从字符到字符串的转换

C++ C++;从字符到字符串的转换,c++,C++,代码如下: int main(){ string word= "word"; char ciphered[word.length()]; for(int i=0; i<word.length(); i++) { int current_position = (int) word[i]; int new_position = current_position + 2; char this_char = (char) new_position; ciphered[i] = this_c

代码如下:

int main(){

 string word= "word";

char ciphered[word.length()];

for(int i=0; i<word.length(); i++)
{
int current_position = (int) word[i];
int new_position = current_position + 2;
char this_char = (char) new_position;
ciphered[i] = this_char;

}



string str(ciphered);

cout << str << endl ;

}
intmain(){
string word=“word”;
字符加密[word.length()];
对于(int i=0;i首先:

char ciphered[word.length()];
不是合法的C++代码,虽然GCC可以接受它。但是第二,您不需要真的char数组,因为您可以用“代码> STD::String 本身:

访问单个符号。
string word= "word";
string ciphered( word.length(), ' ' );

for(int i=0; i<word.length(); i++)
{
    ciphered[i] = word[i] + 2;
}
cout << ciphered << endl;
string word=“word”;
已加密的字符串(word.length(),“”);

对于(int i=0;i@Kon与它的实现一样,代码<垃圾邮件>。但是公平地说,它实际上并不是C++。参见VLA。你可能只想通过适当的编译器运行它。注意:你不必通过java和/或C编译器运行C++代码。现代C++编译器直接生成二进制文件。C++中,字符串以null(char 0)终止。循环可能覆盖字符串终止符,因此您看到一些垃圾字符在未初始化的内存中。使用<代码> siZOFF()/Cype >通过设置字符数来避免这一点。我是懒惰的,我使用的是MIW编译器。
string word= "word";
string ciphered( word.length(), ' ' );

for(int i=0; i<word.length(); i++)
{
    ciphered[i] = word[i] + 2;
}
cout << ciphered << endl;