我有一个任务,它希望我从用户那里获得一个cstyle字符串(只有一个单词),然后使用一个函数来反转它的字母。该函数必须接受两个参数,第一个是目标字符串,另一个是源字符串。它将获取源字符串中的任何内容,反转它,然后将反转的版本存储在第二个字符串中。
但每次编译时,我都会键入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)
{
}发布于 2018-04-11 10:51:31
指针会使工作变得更困难,如果你不需要它们,就不要使用它们。由于您的任务是反转字符串,因此只需使用string即可。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;EDIT:为了满足您的赋值要求(有一个带有两个参数和一个单词字符串的函数),您可以像这样轻松地更改上面的代码:
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]);
}
} 发布于 2018-04-11 18:35:06
不需要使用指针将一个字符数组反转到另一个字符数组。
void Reverse(char *str2, char *str1)
{
int len = strlen(str1);
for(int n = 0; n< len; n++)
{
str2[n] = str1[len-n-1];
}
}https://stackoverflow.com/questions/49765456
复制相似问题