博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言 字符串字符反向储存_C ++中的反向字符串
阅读量:2529 次
发布时间:2019-05-11

本文共 6000 字,大约阅读时间需要 20 分钟。

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 
#include
#include
using namespace std; int main() { string str = "Journal Dev reverse example"; reverse(str.begin(), str.end()); cout<<"\n"<

Output:

输出:

Use Of Reverse string in c++
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
#include
using namespace std; int main() { char str[] ="Journal Dev reverse example"; strrev(str); cout<<"\n"<

Output:

输出:

Reverse Using Strrev reverse string in C++
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
#include
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<

Output:

输出:

Reverse Print
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
#include
#include
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<

Output:

输出:

User defined My_rev() 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)

翻译自:

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

转载地址:http://dwlzd.baihongyu.com/

你可能感兴趣的文章
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>