简体   繁体   English

我做错了什么? 反向字符串 C++

[英]what am i doing wrong?reverse string c++

so I want to do the simplest thing in c++ , reverse a string (store the new string) and than print it所以我想在 c++ 中做最简单的事情,反转一个字符串(存储新字符串)然后打印它

my code is :我的代码是:

char a[size] , reverse[size];
strcpy(a,"dlow olleh " );
for (int i = 0 ;  i <= strlen(a); i++) {
   reverse[i]= a[strlen(a)-i];
}
cout << reverse ;

I must note that when cout << reverse[i] ;我必须注意,当 cout << reverse[i] ; is inside the for loop every thing wotks fine , but when I wwant to print it as a string it just don't , I cant under stand what ive missed cout << reverse[i] ;在 for 循环中,一切都很好,但是当我想将它打印为字符串时,它只是不这样做,我无法理解我错过了什么 cout << reverse[i] ;

what am i doing wrong?我究竟做错了什么?

You are using arrays of char and functions of the C Standard Library to manipulate strings in C++.您正在使用 C 标准库的char数组和函数来操作 C++ 中的字符串。

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    std::string foo{ "Hello, World!" };
    std::string bar{ foo };
    std::reverse(bar.begin(), bar.end());
    std::cout << '\"' << foo << "\" ==> \"" << bar << "\"\n";
}

If – for some reason beyond my comprehension – you *have to* do it by foot, do it in an idiomatic way and provide an interface that takes a pair of iterators:如果——出于某种超出我理解的原因——你*必须*步行,以惯用的方式完成并提供一个带有一对迭代器的接口:

#include <algorithm>

void str_reverse(char *begin, char *end)
{
    while (begin < end)
        std::swap(*begin++, *--end);
}

// ...

#include <cstring>
// ...
char foo[]{ "Hello, World!" };
str_reverse(foo, foo + std::strlen(foo));

If you can't use <algorithm> for whatever reason implement your own swap() :如果您因任何原因不能使用<algorithm>实现您自己的swap()

template<typename T>
void swap(T &a, T &b)
{
    T tmp{ a };
    a = b;
    b = tmp;
}

In this loop在这个循环中

for (int i = 0 ;  i <= strlen(a); i++){
       reverse[i]= a[strlen(a)-i];

you are accessing characters beyond the actual characters of the strings.您正在访问超出字符串实际字符的字符。

For example when i is equal to 0 you are coping the terminating zero character from the string a into the first position of the string reverse .例如,当i等于 0 时,您将字符串a的终止零字符处理到字符串reverse的第一个位置。

reverse[0]= a[strlen(a)-0];

the code can be written simpler without for example reduntant calls of the function strlen .代码可以写得更简单,例如无需对函数strlen进行冗余调用。

char a[size], reverse[size];
strcpy( a, "dlrow olleh" );

size_t i = 0;
for ( size_t n = strlen( a ); i < n; i++ ) 
{
    reverse[i] = a[n - i - 1];
}
reverse[i] = '\0';

std::cout << reverse << '\n';

Pay attention to that there is the standard algorithm std::reverse_copy that does the same task.请注意,有执行相同任务的标准算法std::reverse_copy

Below there is a demonstrative program.下面是一个演示程序。

#include <iostream>
#include <algorithm>
#include <cstring>

int main() 
{
    const size_t SIZE = 20;
    char a[SIZE], reverse[SIZE];

    std::strcpy( a, "dlrow olleh" );    

    std::cout << a <<'\n';

    auto it = std::reverse_copy( a, a + strlen( a ), reverse );
    *it = '\0';

    std::cout << reverse <<'\n';

    return 0;
}

The program output is程序输出是

dlrow olleh
hello world

The first you copy when reversing the string is in fact the null terminator so when you are printing it to console it will not show up since the null terminator is the first in the array so you want to do this instead您在反转字符串时复制的第一个实际上是空终止符,因此当您将其打印到控制台时,它不会显示出来,因为空终止符是数组中的第一个,因此您想要这样做

int size = 12;
char a[12], reverse[12];
strcpy(a, "dlow olleh ");
for (int i = 0; i < strlen(a); i++) {
    reverse[i] = a[strlen(a) - (i+1)];
}
reverse[strlen(a)] = '\0';
cout << reverse;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM