使用字符数组反转字符串 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24372480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse String C++ using char array
提问by StackPointer
I wrote a simple C++ program to reverse a string. I store a string in character array. To reverse a string I am using same character array and temp variable to swap the characters of an array.
我编写了一个简单的 C++ 程序来反转字符串。我在字符数组中存储一个字符串。要反转字符串,我使用相同的字符数组和临时变量来交换数组的字符。
#include<iostream>
#include<string>
using namespace std;
void reverseChar(char* str);
char str[50],rstr[50];
int i,n;
int main()
{
cout<<"Please Enter the String: ";
cin.getline(str,50);
reverseChar(str);
cout<<str;
return 0;
}
void reverseChar(char* str)
{
for(i=0;i<sizeof(str)/2;i++)
{
char temp=str[i];
str[i]=str[sizeof(str)-i-1];
str[sizeof(str)-i-1]=temp;
}
}
Now this method is not working and, I am getting the NULL String as result after the program execution.
现在这个方法不起作用,我在程序执行后得到 NULL String 作为结果。
So I want to know why I can't equate character array, why wouldn't this program work. And what is the solution or trick that I can use to make the same program work?
所以我想知道为什么我不能等同于字符数组,为什么这个程序不起作用。我可以使用什么解决方案或技巧来使相同的程序工作?
回答by Bill Lynch
sizeof(str)
does not do what you expect.
sizeof(str)
不会做你期望的。
Given a char *str
, sizeof(str)
will not give you the length of that string. Instead, it will give you the number of bytes that a pointer occupies. You are probably looking for strlen()
instead.
给定一个char *str
,sizeof(str)
不会给你那个字符串的长度。相反,它会给你一个指针占用的字节数。您可能正在寻找strlen()
。
If we fixed that, we would have:
如果我们解决了这个问题,我们将有:
for(i=0;i<strlen(str)/2;i++)
{
char temp=str[i];
str[i]=str[strlen(str)-i-1];
str[strlen(str)-i-1]=temp;
}
This is C++, use std::swap()
这是 C++,使用 std::swap()
In C++, if you want to swap the contents of two variables, use std::swap
instead of the temporary variable.
在 C++ 中,如果要交换两个变量的内容,请使用std::swap
代替临时变量。
So instead of:
所以而不是:
char temp=str[i];
str[i]=str[strlen(str)-i-1];
str[strlen(str)-i-1]=temp;
You would just write:
你会写:
swap(str[i], str[sizeof(str) - i - 1]);
Note how much clearer that is.
请注意这有多清楚。
You're using C++, just use std::reverse()
您正在使用 C++,只需使用 std::reverse()
std::reverse(str, str + strlen(str));
Global variables
全局变量
It's extremely poor practice to make variables global if they don't need to be. In particular, I'm referring to i
about this.
如果不需要,将变量设为全局是非常糟糕的做法。特别是,我指的是i
这个。
Executive Summary
执行摘要
If I was to write this function, it would look like one of the two following implementations:
如果我要编写此函数,它将类似于以下两种实现之一:
void reverseChar(char* str) {
const size_t len = strlen(str);
for(size_t i=0; i<len/2; i++)
swap(str[i], str[len-i-1]);
}
void reverseChar(char* str) {
std::reverse(str, str + strlen(str));
}
When tested, both of these produce dlrow olleh
on an input of hello world
.
测试时,这两者都dlrow olleh
在输入为 时产生hello world
。
回答by leemes
The problem is that within your function, str
is not an array but a pointer. So sizeof
will get you the size of the pointer, not the length of the array it points to. Also, even if it gave you the size of the array, that is not the length of the string. For this, better use strlen
.
问题是在您的函数中,str
不是数组而是指针。所以sizeof
会得到指针的大小,而不是它指向的数组的长度。此外,即使它给了您数组的大小,也不是字符串的长度。为此,最好使用strlen
.
To avoid multiple calls to strlen
, give the function another parameter, which tells the length:
为避免多次调用strlen
,请为该函数提供另一个参数,该参数告知长度:
void reverseChar(char* str, int len)
{
for(i=0; i<len/2; i++)
{
char temp=str[i];
str[i]=str[len-i-1];
str[len-i-1]=temp;
}
}
and call it with
并调用它
reverseChar(str, strlen(str))
reverseChar(str, strlen(str))
Another improvement, as mentioned in the comments, is to use std::swap
in the loop body:
正如评论中提到的,另一个改进是std::swap
在循环体中使用:
void reverseChar(char* str, int len)
{
for(i=0; i<len/2; i++)
{
std::swap(str[i], str[len-i-1]);
}
}
Also, there is std::reverse
which does almost exactly that.
此外,std::reverse
几乎完全相同。
回答by Kartik
//reverse a string
#include<iostream>
using namespace std;
int strlen(char * str) {
int len = 0;
while (*str != 'int main()
{
string str;
cout << "Enter a string: " << endl;
getline(cin, str);
for (int x = str.length() - 1; x > -1; x--)
{
cout << str[x];
}
return 0;
}
') {
len++;
str++;
}
return len;
}
void reverse(char* str, int len) {
for(int i=0; i<len/2; i++) {
char temp=str[i];
str[i]=str[len-i-1];
str[len-i-1]=temp;
}
}
int main() {
char str[100];
cin.getline(str,100);
reverse(str, strlen(str));
cout<<str<<endl;
getchar();
return 0;
}
回答by Cjolsen06
If I were you, I would just write it like so:
如果我是你,我会这样写:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
cout << "Enter a string bro: \n";
gets_s(str);
for (int i = strlen(str) - 1; i > -1; i--)
{
cout << str[i];
}
}
This is a very simple way to do it and works great.
这是一种非常简单的方法,并且效果很好。