c语言 字符串字符反向储存

In many situations, we may need to reverse a string in C++ programming. It may include just printing a reversed string. Or maybe in some cases need to reverse the string permanently at its address.

在许多情况下,我们可能需要在C ++编程中反转字符串 。 它可能包括仅打印反向字符串。 或者,在某些情况下,可能需要永久性地在其地址处反转字符串。

In this tutorial, we are going to learn how we can accomplish both the tasks. That too using different pre-defined as well as user-defined functions.

在本教程中,我们将学习如何完成这两个任务。 那也使用了不同的预定义以及用户定义的函数。

在C ++中反转字符串 (Reversing a String in C++)

Reversing a string refers to the operation on a string, by which the sequence of characters in it gets reversed. For example, consider ‘str’ contains a string “JournalDev” in it.

反转字符串是指对字符串进行的操作,通过该操作可以反转字符串中的字符序列。 例如,考虑“ str”在其中包含字符串“ JournalDev”

After the reversal operation takes place on the string ‘str’, the content should get reversed. Hence now, ‘str’ should contain the string “veDlanruoJ”.

在对字符串'str'进行反向操作之后,内容应该被反向。 因此,现在'str'应该包含字符串“ veDlanruoJ”

Now let us see how we can perform this reverse operation on C++ strings using various techniques.

现在让我们看看如何使用各种技术对C ++字符串执行此反向操作。

在C ++中使用reverse()函数 (Using reverse() function in C++)

The built-in reverse function reverse() in C++ directly reverses a string. Given that both bidirectional begin and end iterators are passed as arguments.

C ++中内置的反向函数reverse()直接反向字符串。 假定双向的开始结束迭代器都作为参数传递。

This function is defined in the algorithm header file. The code given below describes the use of reverse() function,

该函数在算法头文件中定义。 下面给出的代码描述了reverse()函数的用法,


#include <algorithm>
#include<iostream>
#include<string>
using namespace std;
int main()
{ string str = "Journal Dev reverse example"; reverse(str.begin(), str.end()); cout<<"\n"<<str; return 0;
}

Output:

输出:

Use Of reverse()
使用reverse()

使用strrev() (Using strrev())

strrev() is a pre-defined function in C++, defined inside the cstring.h header file. It is extensively applicable for reversing any C-string(character array).

strrev()是C ++中的预定义函数,在cstring.h头文件中定义。 它广泛适用于反转任何C字符串(字符数组)。

Further, it only requires the base address of the string as its argument and reverses the string accordingly. Let us see how we can use the strrev() function in C++ to reverse strings.

此外,它仅需要字符串的基地址作为其参数,并相应地反转字符串。 让我们看看如何在C ++中使用strrev()函数反转字符串。


#include<iostream>
#include<cstring>
using namespace std;
int main()
{ char str[] ="Journal Dev reverse example"; strrev(str);cout<<"\n"<<str; return 0;
}

Output:

输出:

Reverse Using strrev()
反向使用strrev()

The code above illustrates the working of the function strrev() very well. For a string ‘str’, the function reverses it successfully as we can see in the output itself.

上面的代码很好地说明了函数strrev()的工作。 对于字符串“ str”,该函数成功将其反转,正如我们在输出本身中看到的那样。

反向打印字符串 (Printing a String in reverse)

In certain cases, we may not need to change the string but only print it in a reversed manner. This could be for constant strings that cannot be modified. We can print any string in a reversed pattern by using a loop. Let us see how.

在某些情况下,我们可能不需要更改字符串,而只需以相反的方式打印它即可。 这可能是无法修改的常量字符串。 我们可以使用循环以反向模式打印任何字符串。 让我们看看如何。


#include<iostream>
#include<string>
using namespace std;
int main()
{ string str="Journal Dev reverse example"; int i;cout<<"Printing string in reverse\n";for(i = str.length() - 1; i >= 0; i--){cout<<str[i];}return 0;
}

Output:

输出:

Reverse Print
反向打印
  • In the above-given code, we have firstly initialized a string ‘str’.在上面给出的代码中,我们首先初始化了一个字符串'str'。
  • Inside the for loop, while printing the string, note that we have initialized the iterator ‘i’ with a value str.length()-1. This means that we print the string character by character but, starting from the last index.在for循环内,在打印字符串时,请注意,我们已使用值str.length()-1初始化了迭代器'i' 。 这意味着我们从最后一个索引开始逐字符打印字符串。
  • Note: length() returns the length of a string. So, for printing a string in reverse we should consider the last index which should be length()-1, since indexing starts from ‘0’ in a character array.注意: length()返回字符串的长度。 因此,对于反向打印字符串,我们应考虑最后一个索引,该索引应为length()-1 ,因为索引是从字符数组中的“ 0”开始的。

制作自己的字符串反转函数My_rev() (Making our own string reversing function My_rev())

Until now we learned how we can print a string in reverse as well as reverse the string using different pre-defined functions. Now let us create or define our own function named My_rev() to reverse a given string.

到现在为止,我们了解了如何反向打印字符串以及如何使用不同的预定义函数反转字符串。 现在让我们创建或定义自己的名为My_rev()的函数以反转给定的字符串。


#include<iostream>
#include<string>
#include<cstring>
using namespace std;
char *My_rev(char *str)
{int i,len=0,n;char temp;len=strlen(str);n=len-1;for(i = 0; i <=(len/2); i++){temp=str[i];str[i]=str[n];str[n]=temp;n--;}return str;
}
int main()
{ char My_string[]="Journal Dev reverse example";cout<<"Reverse string using My_rev()...\n";My_rev(My_string);cout<<My_string;return 0;
}

Output:

输出:

User-defined My_rev() function Output
用户定义的My_rev()函数输出
  • In the code above, My_rev() is a function that reverses a string, given the base address of the string is passed as an argument.在上面的代码中, My_rev()是一个反转字符串的函数,给定字符串的基地址作为参数传递。
  • Inside the My_rev() function, *str is a pointer that stores the base address of the provided string. In our case, str points to the first element of the string My_string.在My_rev()函数内部, * str是一个指针,用于存储所提供字符串的基地址。 在我们的例子中,str指向字符串My_string的第一个元素。
  • len stores the length of the string. Whereas, n is the index of the last element.len存储字符串的长度。 而n是最后一个元素的索引。
  • In the function, we try to swap individual characters of the string, from both ends. That means we go on swapping elements from the 0th and nth index until we get to the (len/2)th position. In the code above, the for loop does this swapping for us which technically reverses the string.在函数中,我们尝试从两端交换字符串的各个字符。 这意味着我们将继续从第0 索引和第n个索引交换元素,直到到达第(len / 2)个位置。 在上面的代码中, for循环为我们执行了此交换操作,从技术上讲,它反转了字符串。
  • In the end, we return the base address str to the main() function. Where the string is printed using the cout function.最后,我们将基地址str返回给main()函数。 使用cout函数在字符串上打印的位置。

参考资料 (References)

  • https://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-chttps://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c
  • https://www.journaldev.com/35738/string-length-in-c-plus-plushttps://www.journaldev.com/35738/string-length-in-c-plus-plus

翻译自: https://www.journaldev.com/35816/reverse-string-c-plus-plus

c语言 字符串字符反向储存

c语言 字符串字符反向储存_C ++中的反向字符串相关推荐

  1. C语言在读取txt类型文件中的汉字字符串出现乱码的解决办法

    题目 C语言在读取txt类型文件中的汉字字符串出现乱码的解决办法 以下是本篇文章正文内容,欢迎朋友们进行指正,一起探讨,共同进步.--来自考研路上的lwj 一.前言 当我们在练习文件这一章节时,因为需 ...

  2. python原生字符串可以参与比较_正则表达式中对于原生字符串的理解

    在正则表达式中,有些字符是有特殊意义的字符.因此如果想要匹配这些字符,那么就必须使用反斜杠进行转义.比如$代表的是以...结尾,如果想要匹配$,那么就必须使用\$.示例代码如下: text = &qu ...

  3. java中转json字符串_如何在Java中转义JSON字符串-Eclipse IDE技巧

    java中转json字符串 在Java应用程序中工作或进行JSON解析时,通常很常见的做法是从某些资源(例如RESTful Web服务)中复制粘贴JSON字符串,然后使用Jackson库解析JSON. ...

  4. oracle 截取字符串中间_oracle截取字段中的部分字符串

    使用Oracle中Instr()和substr()函数: 在Oracle中可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符. 其语法为: instr(sourceString,de ...

  5. c#截取字符串后几位_C#几种截取字符串的方法小结 (摘抄)

    1.根据单个分隔字符用split截取 例如 string st="GT123_1"; string[] sArray=st.split("_"); 即可得到sA ...

  6. java文件中查找字符串_Java 在本地文件中查找固定字符串

    适用范围:只适用于在文本文档中查找(如,txt.java.c等等,并不适用与doc.xls等等这些文件),可嵌套文件夹.但是对中文不支持. 例如:文件夹:F:/demo 子文件夹:F:/demo/er ...

  7. python字符串find函数实现_python中实现查找字符串的find函数

    原博文 2018-06-19 19:26 − 第五题:自己实现一个字符串的find函数1.在一个字符串中查找另一个字符串2.找到了返回第一次出现的位置3.没找到返回-14.参数s1为源字符串,参数s2 ...

  8. c语言atoll函数怎么用_C ++中带有示例的atoll()函数

    c语言atoll函数怎么用 C ++ Atoll()函数 (C++ atoll() function) atoll() function is a library function of cstdli ...

  9. c语言 函数的参数传递示例_C ++中带有示例的nearint()函数

    c语言 函数的参数传递示例 C ++附近的int()函数 (C++ nearbyint() function) nearbyint() function is a library function o ...

最新文章

  1. R语言数据包自带数据集之survival包的lung数据集字段解释、数据导入实战
  2. 【以前的空间】主席树
  3. url参数传递 java_URL中文参数传递问题
  4. 独家揭秘!史上最强中文NLP预训练模型 | 直播报名中
  5. uglify压缩angular控制器注意
  6. 使用VS Code开发调试.NET Core 2.0
  7. 大牛深入讲解!高并发你真的理解透彻了吗
  8. php xssclean,php – Codeigniter xss_clean困境
  9. ORM Designer for Rails Demo
  10. 红薯叶有什么营养价值?
  11. 用Welford算法实现LN的方差更新
  12. python生成矢量图_Jupyter Notebook输出矢量图实例
  13. 表白神器(VBS编程)
  14. html使用iframe包含pdf文件,react项目利用iframe显示pdf文件并打印
  15. 英伟达显卡安装老驱动388.71
  16. 百度大脑3月新品推荐:EasyDL视频目标追踪全新发布
  17. 如何时重启打印机服务bat命令,打印机重启服务脚本 Win7打印机服务怎么开启 Win7开启打印机服务的设置的两种方法
  18. 微信公众账号分类入门知识
  19. 1.2 最短路算法的多用
  20. 伽罗瓦死了,可是数学还活着

热门文章

  1. System.Web.AspNetHostingPermission 类型的权限已失败
  2. 读书笔记 - 《皇上走了》
  3. 禁止minigui 3.0的屏幕保护
  4. 微软云平台 Azure简介 (三)Windows Azure 存储概述
  5. [转载] python 中numpy快速去除nan, inf的方法
  6. [转载] Java基础之构造方法(函数)何时被调用
  7. centos7卸载旧jdk安装新jdk1.8
  8. WLC-生成CSR操作
  9. Python3模块: hashlib
  10. markown编辑器截图粘贴预览,并将图片传至七牛云