gpt4 book ai didi

c++ - 空终止字符数组(c 字符串)没有 for 循环不打印

转载 作者:行者123 更新时间:2023-11-28 02:38:27 25 4
gpt4 key购买 nike

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

好了,这个程序就快结束了。除了 reverseCString 函数外,我所有的用户定义函数都工作正常。当我运行程序并输入字符串“hello”时,我可以选择菜单选项“rev”来反转字符串。然后我的主函数调用 reverseCString 函数,并在我的主函数中使用 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”开头的字符串。当然打印出来不会产生任何结果。

关于c++ - 空终止字符数组(c 字符串)没有 for 循环不打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26771698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com