Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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++ - Fatal编程技术网

C++ 复制数组的元素以创建新词

C++ 复制数组的元素以创建新词,c++,C++,我正在尝试编写一个代码,它将执行以下操作: Input: Sample 这就是我到目前为止所做的: #include <string> #include <bits/stdc++.h> #include <cstring> using namespace std; int main() { string s; cout << "Input a word: "; cin >> s;

我正在尝试编写一个代码,它将执行以下操作:

Input: Sample
这就是我到目前为止所做的:

#include <string>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

int main()
{
    string s;
 
    cout << "Input a word: ";
    cin >> s;
    
    int n = s.length();
 
    char word[n + 1];
 
    strcpy(word, s.c_str());
 
    for (int i = 0; i < n; i++)
    {
        if (i > i)
        {
            int x = i * i;
            cout << word[x];
        }
    }
 
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串s;
cout>s;
int n=s.长度();
字符字[n+1];
strcpy(word,s.c_str());
对于(int i=0;ii)
{
int x=i*i;

cout我会这样做,一个循环用于单词,另一个循环用于每个字母的重复量:

int main()
{
    string s;
 
    cout << "Input a word: ";
    cin >> s;
    
    int n = s.length();
 
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= i; ++j) {
            cout << s[i];
        }
    }
    cout << endl;
 
    return 0;
}
intmain()
{
字符串s;
cout>s;
int n=s.长度();
对于(int i=0;i对于(int j=0;j如果查看输出,可以利用一种模式。第一个字母打印1次,第二个字母打印2次,第三个字母打印3次

找到模式是获得算法的重要一步

我对下面的代码做了一些更改,并注意到了它们。最大的更改是for循环。我只是利用
std::string
构造函数在原始字符串中添加每个字符的正确字母数

#包括
//#包括//改变:只是不要
//#包含//更改:不需要
#include//CHANGED:包括您使用的内容
//使用命名空间std;//更改:错误做法
int main(){
std::字符串s;
std::cout>s;
//更改:调整算法以适应观察到的模式
std::字符串新词;
对于(std::size_t i=0;istd::cout非常感谢您的帮助!同时,我试着自己写一些东西,我写了一个工作的,看起来像这样:

#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

int main()
{
    string s;
 
    cout << "Input a word: ";
    cin >> s;
    
    int n = s.length();
 
    char word[n + 1];
 
    strcpy(word, s.c_str());
 
    for (int i = 0; i < n; i++)
    {
        for (int k = 0; k < (i + 1); k++)
        {
            cout << word[i];
        }
    }
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串s;
cout>s;
int n=s.长度();
字符字[n+1];
strcpy(word,s.c_str());
对于(int i=0;icout作为一种基本策略,尝试读取输入并创建一个新字符串,在循环中为正在读取的字符追加该字符串。是否有任何理由使用类似于
char word[n+1]的字符数组
而不是像
std::string word;
那样的另一个
std::string
?不相关的
#include
表示你不知道
#include
做了什么。留下并
Saammmppppllllleeeeee
#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

int main()
{
    string s;
 
    cout << "Input a word: ";
    cin >> s;
    
    int n = s.length();
 
    char word[n + 1];
 
    strcpy(word, s.c_str());
 
    for (int i = 0; i < n; i++)
    {
        for (int k = 0; k < (i + 1); k++)
        {
            cout << word[i];
        }
    }
    return 0;
}