繁体   English   中英

空终止的char数组(c字符串)不打印而没有for循环

[英]null terminated char array (c-string) not printing without for loop

**我不能在此程序中使用向量或标准库中的任何函数。 因此,为什么我要自己写呢。

好了,这个程序即将完成。 我所有的用户定义函数都可以正常工作,但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;
    }

您的长度逻辑已损坏。 假设该字符串包含“ 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”开头的字符串。 当然,打印出来不会产生任何效果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM