(一)用基本的数组实现

#include "stdafx.h"
#include <stdio.h>
#include <string.h> int main()
{
char ch1[10] = "abcde", ch2[10] = {0};
int n=0, i=0, j=0;
n = strlen(ch1);
for(i = n-1; i>=0; i--)
{
ch2[j] = ch1[i];
j++;
}
printf("%s\n%s\n", ch1, ch2);
return 0;
}

(二)加入向量vector, vector是具有方向的矢量容器,使用时,需include <vector>

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <vector>
int main()
{
char ch1[] = "abcde", ch2[] = {};
int n=, i=, j=;
n = strlen(ch1);
vector <char> cVec(ch1, ch1+n);
for(i = cVec.size()-; i>=; i--)
{
ch2[j] = ch1[i];
j++;
}
printf("%s\n%s\n", ch1, ch2);
return ;
}

(三)加入迭代器(iterator), iterator是一中检查容器内元素并遍历元素的数据类型,每个容器都可以定义自己的迭代器。

使用迭代器,需include <iterator>

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <vector>
#include <iterator> int main()
{
char ch1[] = "abcde", ch2[] = {};
int n=, i=, j=;
n = strlen(ch1);
vector <char> cVec(ch1, ch1+n);
vector <char>::reverse_iterator cRIter;
for(cRIter=cVec.rbegin(); cRIter!=cVec.rend(); cRIter++)
{
ch2[j] = *cRIter;//同时也可更改*cRIter为cRIter[0];
j++;
}
printf("%s\n%s\n", ch1, ch2);
return ;
}

(四)使用双向链表list,list可以被视为一个双向链表,每个元素都具有前后元素的链接

1. (同上为反向迭代器)

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <list>
#include <iterator> int main()
{
char ch1[] = "abcde", ch2[] = {};
int n=, i=, j=;
n = strlen(ch1);
list<char> cList(ch1, ch1+n);
list<char>::reverse_iterator cRIter; for(cRIter=cList.rbegin(); cRIter!=cList.rend(); cRIter++)
{
ch2[j] = *cRIter;
j++;
}
printf("%s\n%s\n", ch1, ch2);
return ;
}

(四)使用双向链表list,正向迭代器

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <list>
#include <iterator> int main()
{
char ch1[] = "abcde", ch2[] = {};
int n=, i=, j=;
n = strlen(ch1);
list<char> cList(ch1, ch1+n);
list<char>::iterator cIter = cList.end();
cIter--;
for(cIter; cIter!=cList.begin();cIter--)
{
ch2[j] = *cIter;
j++;
}
if(cIter==cList.begin())
{
ch2[j] = *cIter;
}
printf("%s\n%s\n", ch1, ch2);
return ;
}

以上所有输出结果为:

(五)使用双向链表list,iterator正向迭代器复制的例子;

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <list>
#include <iterator> int main()
{
char ch1[] = "abcde", ch2[] = {};
int n=, i=, j=;
n = strlen(ch1);
list<char> cList(ch1, ch1+n);
list<char>::iterator cIter; for(cIter=cList.begin(); cIter!=cList.end(); cIter++)
{
ch2[j] = *cIter;
j++;
}
printf("%s\n%s\n", ch1, ch2);
return ;
}

输出结果为:

C++基础--字符串倒序输出的更多相关文章

  1. Java基础知识强化08:将字符串倒序输出(包括空格)的几种方法

    1.最容易想到的估计就是利用String类的toCharArray(),再倒序输出数组的方法了: package himi.hebao05; public class TestDemo02 { pub ...

  2. Java实现字符串倒序输出的几种方法

    1. 最容易想到的估计就是利用String类的toCharArray(),再倒序输出数组的方法了. import javax.swing.JOptionPane; public class Rever ...

  3. Java基础 -- 字符串(格式化输出、正则表达式)

    一 字符串 1.不可变String String对象是不可变的,查看JDK文档你就会发现,String类中每一个看起来会修改String值的方法,实际上都是创建一个全新的String对象,以包含修改后 ...

  4. C语言-实现字符串倒序输出

    方法1: Action(){//倒序输出 char *src="abcdefgh123"; char *desc; desc=(char *)malloc(100*sizeof(c ...

  5. Java基础学习【字符串倒序输出+排序】

    字符串逆序输出 import java.util.*; public class Main{ public static void main(String [] args) { //字符串逆序输出 S ...

  6. C++如何输入单行和多行带空格的字符串并将字符串倒序输出

    首先,我们知道在C++中,空格或者回车被认为是字符串的分割标志,使用cin输入string类的字符串时遇到会自动停止接收输入 例如,当如下程序时: #include <bits/stdc++.h ...

  7. Python基础 | 字符串格式化输出及print()函数介绍

    在写代码时,我们会经常与字符串打交道,Python中控制字符串格式通常有三种形式,分别是使用str%,str.format(),f-str,用法都差不多,但又有一些细微之差. 一起来看看吧~~~ 一. ...

  8. Java开发中经典的小实例-(字符串倒序输出)

    public class Test12 {    public static void main(String[] args) {        // TODO Auto-generated meth ...

  9. C#字符串的倒序输出

    介绍 在本文中,我将演示如何将字符串的单词倒序输出.在这里我不是要将“John” 这样的字符串倒序为成“nhoJ”,.这是不一样的,因为它完全倒序了整个字符串.而以下代码将教你如何将“你 好 我是 缇 ...

随机推荐

  1. mtd-util

    1.1.4.1. mtd-util简介 mtd-util,即mtd的utilities,是mtd相关的很多工具的总称,包括常用的mtdinfo,flash_erase, flash_eraseall, ...

  2. 转载Java NIO中的Files类的使用

    Java NIO中的Files类(java.nio.file.Files)提供了多种操作文件系统中文件的方法. Files.exists() Files.exits()方法用来检查给定的Path在文件 ...

  3. P3813 [FJOI2017]矩阵填数

    传送门 矩阵很大,但是发现 $n$ 很小,从这边考虑,对于一个一堆小矩阵放在一起的情况 考虑把每一块单独考虑然后方案再乘起来 但是这些奇怪的东西很不好考虑 所以暴力一点,直接拆成一个个小块 但是这样我 ...

  4. (找到最大的整数k使得n! % s^k ==0) (求n!在b进制下末尾0的个数) (区间满足个数)

    题目:https://codeforces.com/contest/1114/problem/C 将b分解为若干素数乘积,记录每个素数含多少次方 b = p1^y1·p2^y2·...·pm^ym. ...

  5. Phyton Flask框架学习记录。

    注意:在左侧菜单栏(在JQuery插件库下载的)右边是采用<iframe> 标签嵌入其他页面,此时标签的src应用用后台中的方法名称(本人测试用的是无参数的方法), 而页面跳转window ...

  6. 6.centos7 gitblit

    1,安装注意事项 1)必须将gitblit安装在/opt/gitblit这个目录下,因为 服务启动的脚本里面默认就是这个路径 如果不按照这个路径安装需要修改脚本 mkdir -p /opt/gitbl ...

  7. hcheck 脚本

    hcheck.sql - Script to Check for Known Problems in Oracle8i, Oracle9i, Oracle10g, Oracle 11g and Ora ...

  8. oracle12C--DG FAR SYNC 部署(前提为搭建好12C的DG)

    <<往期12CDG搭建>> 一,理解同步异步模式 01, 使用LGWR 进程的SYNC 方式 1)Primary Database 产生的Redo 日志要同时写到日志文件和网络 ...

  9. nginx 配置优化指令

    worker_processes worker_processes指令是用来设计Nginx进程数,官方默认设为1,赋值太多了,将会对系统IO影响效率,降低Nginx服务器性能.但是为了让多核CPU能够 ...

  10. ansible 命令详解{图片详解}

    本文内容来至于http://www.zsythink.net 文件操作模块 命令操作模块 cron 包管理模块