gpt4 book ai didi

c++ - 代码中的运行时错误

转载 作者:太空宇宙 更新时间:2023-11-04 14:55:08 27 4
gpt4 key购买 nike

<分区>

在下面的程序中旋转一个字符串运行时出现错误,请帮忙

代码中没有编译错误

#include <iostream>
#include <cstring>

using namespace std;

void reverseString(char* str, int start, int end)
{
int front = start;
int back = end;
while (front < back)
{
/* swap two variables without
using a temporary one.*/
str[front] ^= str[back];
str[back] ^= str[front];
str[front] ^= str[back];
++front;
--back;
}
return;
}

这部分是用来旋转字符串的

void rotateString(char* str, int k)
{
if (!str || !*str)
return;
int len = strlen(str);
/*Rotating a string by it's length is string itself.*/
k %= len;
reverseString(str, 0, len-1);
reverseString(str, 0, k-1);
reverseString(str, k, len-1);
cout<<str;
return;
}

int main() {
rotateString("abcde",2);
return 0;
}

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