Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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++ 我试图在char数组的中间添加一个空格。不确定如何正确移动它_C++_Arrays_Char_Whitespace - Fatal编程技术网

C++ 我试图在char数组的中间添加一个空格。不确定如何正确移动它

C++ 我试图在char数组的中间添加一个空格。不确定如何正确移动它,c++,arrays,char,whitespace,C++,Arrays,Char,Whitespace,我正在检查一个数组中是否有句尾,然后确保句尾后有空格。我正在尝试将所有数组1移到右侧,以便为空格腾出空间userPara[]只是用户输入的字符数组。我不能使用字符串,但可以使用字符串类 void add_whitespace(char userPara[]) { int len = strlen(userPara); int newlen = len + 1; char temp1; char temp2; char whitespace = ' ';

我正在检查一个数组中是否有句尾,然后确保句尾后有空格。我正在尝试将所有数组1移到右侧,以便为空格腾出空间
userPara[]
只是用户输入的字符数组。我不能使用字符串,但可以使用字符串类

void add_whitespace(char userPara[])
{
    int len = strlen(userPara);
    int newlen = len + 1;
    char temp1;
    char temp2;
    char whitespace = ' ';
    for (int i = 0; i < newlen; i++) {
        if (userPara[i-1] == '.' || userPara[i-1] == '?' || userPara[i-1] == 
            '!' && userPara[i] != ' ') {
            temp1 = userPara[i];
            userPara[i] = whitespace;
            for (int j = i; j < newlen; j++) {
                temp2 = userPara[j+1];
                userPara[j+1] = temp1;
                temp1 = userPara[j+2];
                userPara[j+2] = temp2;
                cout << userPara << endl; //This is for testing
            }
         }
     }
}
void添加_空格(char userPara[])
{
int len=strlen(userPara);
int newlen=len+1;
字符temp1;
字符temp2;
字符空白=“”;
for(int i=0;icout由于在
for
的内部
中将
userPara
的两个元素向右移动,因此应将计数器增加2(
j+=2
而不是
j++
)。 下面是一个示例:

for (int i = 0; i < n; i++) {
    if(userPara [i] == '.')
    {
        temp1 = userPara [i];
        userPara [i] = ' ';
         for (int j = i+1; j < n; j+=2)
         {
            temp2 = userPara [j];
            userPara [j] = temp1;
            temp1 = userPara [j+1];
            userPara [j+1] = temp2;
         }
    break;    
    }

}
for(int i=0;i

正如在评论中所讨论的,请注意,这种方法可能存在盲目增加阵列的问题。

评论不适用于扩展讨论;此对话已经过讨论。