颠倒字符数组 [英] Reverse a character array
本文介绍了颠倒字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
C ++反向数组
如何反转字符数组?像这样:
How can I reverse a character array? Like this:
char word1[10] = "this";
char word2[10] = word1[10] // in reverse
cout<<word2; // I want it to output "siht"
推荐答案
一个简单的循环(因为我不知道 word1
和 word2
的确切类型)?
How about a simple loop (since i do not know the exact type of word1
and word2
)?
for (int i = 0, k = 9; i < 10; i++, k--) {
word2[k] = word1[i];
}
我强烈建议您使用 std :: string
而不是 char
。关于如何使用 std :: string
这里有很多答案。
I highly suggest that you use std::string
instead of char
. There are plenty of answers on how to do this with std::string
here.
这篇关于颠倒字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文