C++ 以null结尾的字符数组(c字符串)在没有for循环的情况下不打印

C++ 以null结尾的字符数组(c字符串)在没有for循环的情况下不打印,c++,visual-c++,c++11,C++,Visual C++,C++11,**我不能在这个程序中使用向量或标准库中的任何函数。这就是为什么我要自己写 好了,这个节目就要结束了。除reverseCString函数外,我所有的用户定义函数都工作正常。当我运行程序时,输入一个字符串“hello”,我可以选择菜单选项“rev”来反转字符串。然后,我的main函数调用reverseCString函数,并使用main中的for循环打印出我的反向c字符串。此时程序运行正常,直到do while循环继续 但是,在调用rv函数之后,程序将循环以继续允许用户修改其字符串,这是应该的。然而

**我不能在这个程序中使用向量或标准库中的任何函数。这就是为什么我要自己写

好了,这个节目就要结束了。除reverseCString函数外,我所有的用户定义函数都工作正常。当我运行程序时,输入一个字符串“hello”,我可以选择菜单选项“rev”来反转字符串。然后,我的main函数调用reverseCString函数,并使用main中的for循环打印出我的反向c字符串。此时程序运行正常,直到do while循环继续

但是,在调用rv函数之后,程序将循环以继续允许用户修改其字符串,这是应该的。然而,我的c字符串在这一点上消失了。如果我使用其他命令/函数,我仍然可以对它进行操作,但是c字符串不能打印到cout

我不知道是什么导致了这一点,但我已经将问题隔离到reverseCString函数

以下是我目前的代码:

#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<string>
#include<math.h>

using namespace std;


void shiftLeft(char szString[], size_t shiftBy)
{
    const char *p = szString;//
    while (*p) ++p;//advance p to point to the the null terminator, found via personal research at http://stackoverflow.com/questions/12201815/what-difference-between-whilepp-while-p-and-whilep
    size_t len = p - szString;//len is set to the size of our c-string 

    if (len > 1 && (shiftBy %= len))
    {
        char *ends[] = { szString+shiftBy, szString+len, szString+(len - shiftBy) };//create a temporary array for storage
        for (size_t i = 0; i < 3; ++i)//use a for loop to flip the first three character
        {
            char *start = szString, *end = ends[i];
            while (start < --end)//now flip the rest of the characters
            {
                char ch = *start;
                *start++ = *end;
                *end = ch;
            }
        }
    }
}


void shiftRight (char szString[], int size, int shiftBy)
{
    if(shiftBy > size){
        shiftBy = shiftBy - size;
    }
    if(size == 1){
        //do nothing, exit function with no change made to myarray
    }
    else{
        char temp;
        //for loop to print the array with indexes moved up (to the right) --> by 2
        for (int i=0; i < size; i++)
        {//EXAMPLE shift by 3  for a c-string of 5
            temp = szString[size-shiftBy];//temp = myarray[2]
            szString[size-shiftBy] = szString[i];//myarray[2] = myarray[i]
            szString[i] = temp;//myarray[i] = temp(value previously at index 2)
        }

    }
}


void reverseCString(char szString[], const size_t& size){
    char temp;
    int i = 0;
    int j = size-1;

    //we can use a simple tep variable to flip everything
    while(i < j)
    {
        temp = szString[i];
        szString[i] = szString[j];
        szString[j] = temp;
        i++;
        j--;
    }
}

    int main(){

        string repeat = "";

        string userInputString;
            cout << "Please eneter your string: " << endl;
            //cin >> ws;
            getline(cin,userInputString);


            char * szString = new char [userInputString.length()+1];
            strcpy (szString, userInputString.c_str());



        do {


            cout << "Your c-string is: " << szString << endl;

            string commandString = "";
            cout << "Please enter a command: ";
            getline(cin,commandString);

            if (commandString[0] == 'L'){
                cout << "You have chosen to shift your string left by " << commandString[1] << " characters." << endl;
                const char * shiftLeftPtr = &commandString[1];
                //convert this value to an int for our function
                int shiftLeftBy = atoi(shiftLeftPtr);
                //run our shifleft function
                shiftLeft(szString,shiftLeftBy);
                //print the results
                cout << "Your c-string, shifted left by " << commandString[1] << endl;
                cout << szString;

            }

            if (commandString[0] == 'R'){
                cout << "You have chosen to shift your string right by " << commandString[1] << " characters." << endl;
                const char * shiftRightPtr = &commandString[1];
                //convert this value to an int for our function
                int shiftRightBy = atoi(shiftRightPtr);
                //run our shiftright function
                shiftRight(szString,userInputString.length(),shiftRightBy);
                //print the results
                cout << "Your c-string, shifted right by " << commandString[1] << endl;
                cout << szString;

            }

            if (commandString.compare("rev") == 0){
                cout << "You have chosen to reverse your string. " << endl;
                //run our reverseArray function
                reverseCString(szString,userInputString.length()+1);

                cout << "Your c-string, reversed: ";
                for(int i = 0; i < userInputString.length()+1; i++){
///////////////////////////right her seems to be my issue
                cout << szString[i];
                }

            }

            if (!(commandString[0] == 'R' || commandString[0] == 'L' || commandString.compare("rev") == 0)){
                cout << "You have entered an invalid selection." << endl;
            }

            cout << "\nEnter 'quit' to close the program, anything else to continue: ";
            getline(cin,repeat);

        }while(repeat.compare("quit") != 0);

        return 0;
    }
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void shiftLeft(字符szString[],大小\u t shiftBy)
{
const char*p=szString//
while(*p)++p;//将p前进到指向空终止符的位置,通过个人研究在http://stackoverflow.com/questions/12201815/what-difference-between-whilepp-while-p-and-whilep
size\u t len=p-szString;//len设置为c字符串的大小
如果(len>1&(移位%=len))
{
char*ends[]={szString+shiftBy,szString+len,szString+(len-shiftBy)};//为存储创建一个临时数组
for(size_t i=0;i<3;++i)//使用for循环翻转前三个字符
{
char*start=szString,*end=ends[i];
while(start<--end)//现在翻转其余字符
{
char ch=*开始;
*开始+=*结束;
*end=ch;
}
}
}
}
void shiftRight(char szString[],int size,int shiftBy)
{
如果(按>大小移动){
shiftBy=shiftBy-尺寸;
}
如果(大小==1){
//不执行任何操作,退出函数,不更改myarray
}
否则{
焦炭温度;
//for循环打印索引上移(向右)-->2的数组
对于(int i=0;icout您的长度逻辑已断开。假设字符串包含“bar\0”。您可以执行以下操作:

            reverseCString(szString,userInputString.length()+1);
现在,长度为3,将4传递给reverseCString。然后发生以下情况:

void reverseCString(char szString[], const size_t& size){
    char temp;
    int i = 0;
    int j = size-1;
现在,
i
是0,
j
是3。所以你交换项目0和3。那么,项目是什么

0 = b
1 = a
2 = r
3 = \0

交换项0和3时,您创建了一个以终止符“\0rab”开头的字符串。当然,打印出来不会产生任何结果。

对不起,没有标准的库函数可以用于反转字符串。您是否使用示例字符串(纸面上或调试器)逐步了解代码的逻辑看看它会做什么?我肯定做了,我已经做了好几次了。函数正在运行,我的c字符串正在反转。当c字符串神奇地消失时,问题似乎出现在我的主字符串的某个地方。所以如果字符串是“bar”,您认为传递给
reverseCString
大小是多少?4.3个字符+空终止符。这是一个以空结尾的字符数组,一个c字符串。我很抱歉没有提到这一点。@DracoM。重点是您想反转空终止符之前的字符(不包括反转中的终止符)