null终止的char数组(c-string)不能在没有for循环的情况下打印

时间:2014-11-06 04:24:23

标签: c++ visual-c++ c++11

**我无法使用此程序中的向量或标准库中的任何函数。因此,为什么我自己写这些。

好的,这个程序刚刚完成。除了reverseCString函数之外,我的所有用户定义函数都正常工作。当我运行程序并输入一个" hello"的字符串时,我可以选择菜单选项" rev"扭转字符串。然后我的main函数调用reverseCString函数,并使用main中的for循环打印出反向的c-string。该程序在此时工作正常,直到do-while循环继续..

在调用rv函数之后,程序循环继续允许用户修改它们的字符串。但是,我的c-string在这一点上消失了。如果我使用其他命令/功能,我仍然可以对它进行操作,但是c-string不会打印到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;
    }

1 个答案:

答案 0 :(得分:2)

你的长度逻辑被打破了。假设字符串包含&#34; bar \ 0&#34;。你这样做:

            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项时,你创建一个以终结符开头的字符串,&#34; \ 0rab&#34;。当然印刷不会产生任何东西。