使用指针 c++ 反转函数中的 cstring 数组

Reversing a cstring array in a function using pointers c++

本文关键字:cstring 数组 函数 指针 c++      更新时间:2023-10-16

我有一个作业,希望我从用户那里获取一个 cstyle 字符串(只有 1 个单词),然后使用一个函数来反转它的字母。该函数必须接受 2 个参数,第一个是目标字符串,另一个是源字符串。它将采用源字符串中的任何内容,反转它,然后将反转的版本存储在第二个字符串中。

但是每次我编译时,我都会输入 hello 它打印出来:\370\365\277\357\376。我刚刚学会了如何使用指针和 cstyle 字符串,所以我真的不知道如何使用它们,我认为这就是弄乱我的代码的原因。我不太了解它,所以我无法弄清楚我哪里出了问题。

如果你知道我做错了什么,请告诉我。谢谢!

#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
using namespace std;
void IsPalindrome(char *cstring);
void Reverse(char *str2[], char *str1[]);
int main() {
// user variables
char str1[81];
char reverse;
char str2[81];
strcpy (str2, str1);
//prompt user to input string
cout << "nPlease input a string (80 chars max): ";
cin >> str1;
cout << "nYour string is: " << str1 << endl;
//call function 
Reverse(str2[81], str1[81]);
//Output reversed string
cout << "Your string reversed is: " << str2 << endl;
cout << "This is a " << "." << endl;

return 0;
}
void Reverse(char *str2, char *str1)
{
char* front, *rear;
int len = strlen(str1);
char temp;
front = str1;
rear = &str1[len - 1];
while(front < rear)
{
temp = *front;
*front = *rear;
*rear = temp;
front++;
rear --;
}
}
void IsPalindrome(char cstring)
{
}

指针使生活更加困难,如果您不需要它们,请不要使用它们。由于您的作业是反转字符串,因此只需使用string.字符串在里面const char*,但更容易处理。

除了代码中的指针和数组问题之外,您还可以使用std::cin从输入中获取字符串。请记住,这样你就无法获得带有空格的字符串(您只会得到第一个单词)。

现在还有一些算法可以为您完成此类任务,但出于教育原因,自己做也不错。

以下是您可以执行的操作,在我提到的代码中,您可以使用现成的算法为您完成任务。

#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <algorithm>
void Reverse(std::string &inputStr);
int main()
{
// user variables
std::string str1 = "";
std::cout << "nPlease input a string (80 chars max): ";
std::getline(std::cin, str1);
std::cout << "nYour string is: " << str1 << std::endl;
Reverse(str1);
//You also can use stl algorithm to reverse string for you and don't do it manually like below, but as it is an assignment it would not be good
//std::reverse(str1.begin(), str1.end());
//Output reversed string
std::cout << "Your string reversed is: " << str1 << std::endl;
std::cout << "This is a " << "." << std::endl;
system("pause");
return 0;
}
void Reverse(std::string &str)
{
int n = str.length();
for(int i = 0; i < n / 2; i++)
{
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
//You also can use stl algorithm to do the swap for you like below
//std::swap(str[i], str[n - i - 1]);
}
}

注意:此示例将反转用户输入的原始字符串,如果要同时具有原始字符串和反向字符串,则需要将输入的副本传递给函数。 像这样:

std::string str2(str1);
Reverse(str2);
std::cout << "Reversed : " << str2;

编辑:为了匹配您的赋值要求(有一个带有两个参数和单个单词字符串的函数),您可以像这样轻松更改上面的代码:

void Reverse(std::string inputStr, std::string &outputStr);
int main()
{
// user variables
std::string str1 = "";
std::string str2 = "";
std::cout << "nPlease input a string (80 chars max): ";
std::cin >>str1;
std::cout << "nYour string is: " << str1 << std::endl;
Reverse(str1, str2);
//You also can use stl algorithm to reverse string for you and don't do it manually like below, but as it is an assignment it would not be good
//std::reverse(str1.begin(), str1.end());
//Output reversed string
std::cout << "Your string original is: " << str1 << std::endl;
std::cout << "Your string reversed is: " << str2 << std::endl;
system("pause");
return 0;
}
void Reverse(std::string inputStr, std::string &outputStr)
{
outputStr = inputStr;
int n = outputStr.length();
for(int i = 0; i < n / 2; i++)
{
char temp = outputStr[i];
outputStr[i] = outputStr[n - i - 1];
outputStr[n - i - 1] = temp;
//You also can use stl algorithm to do the swap for you like below
//std::swap(str[i], str[n - i - 1]);
}   
} 

不需要使用指针将一个字符数组反转到另一个字符数组。

void Reverse(char *str2, char *str1)
{
int len = strlen(str1);    
for(int n = 0; n< len; n++)
{
str2[n] = str1[len-n-1];
}
}