本文共 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.
在本教程中,我们将学习如何完成这两个任务。 那也使用了不同的预定义以及用户定义的函数。
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 ++字符串执行此反向操作。
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:
输出:
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:
输出:
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”,该函数成功将其反转,正如我们在输出本身中看到的那样。
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:
输出:
str.length()-1
. This means that we print the string character by character but, starting from the last index. 在for循环内,在打印字符串时,请注意,我们已使用值str.length()-1
初始化了迭代器'i' 。 这意味着我们从最后一个索引开始逐字符打印字符串。 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”开始的。 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:
输出:
My_rev()
is a function that reverses a string, given the base address of the string is passed as an argument. 在上面的代码中, My_rev()
是一个反转字符串的函数,给定字符串的基地址作为参数传递。 for
loop does this swapping for us which technically reverses the string. 在函数中,我们尝试从两端交换字符串的各个字符。 这意味着我们将继续从第0 个索引和第n个索引交换元素,直到到达第(len / 2)个位置。 在上面的代码中, for
循环为我们执行了此交换操作,从技术上讲,它反转了字符串。 main()
function. Where the string is printed using the cout
function. 最后,我们将基地址str返回给main()
函数。 使用cout
函数在字符串上打印的位置。 翻译自:
c语言 字符串字符反向储存
转载地址:http://dwlzd.baihongyu.com/