如何使用指针反转字符串工作 [英] How does reversing a string with pointers works

查看:112
本文介绍了如何使用指针反转字符串工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它接受一个字符串,并反转,使用前指针和后指针。现在这是下面写的程序。我不明白是while循环。当它分配 * front = * rear,和* rear = temp //这是* front。我们如何分别前后增加和减少。但我们切换他们不是我们?不是前面的后面和后面在前面现在?或者是因为它是指针?

 

include< iostream>
#include< cstdlib>
#include< string>
using namespace std;

void reverse(char * s);
int main()
{
char aString [] =babylonia;

cout<< 请输入字符串<< endl;

reverse(aString);

cout<< aString< endl;


}

void reverse(char * s)
{
char * front,* rear,temp;
front = s;
rear = s + strlen(s) - 1;
while(front< rear)
{
temp = * front;
* front = * rear;
* rear = temp;
front ++;
rear--;
}

}


解决方案

front = s //与 front =& s [0] 相同,数组中第一个字符的地址。

rear ,它指向数组中最后一个字符的地址。

 (这些只是示例地址)
| 0x23fde8 | 0x23fdf0 | 0x23fdf8 | 0x23fe00 | 0x23fe08 | 0x23fe10 | 0x23fe18 | 0x23fe20 | 0x23fe28 |
。---------。---------。---------。--------- .----- ----- ---------。---------。--------- .---------。
| B | A | B | Y | L | O | N | I | A |
'---------'---------'---------'---------'----- ----' ---------'---------'---------'---------'
^ ^

首先, front 地址例如 0x23fde8 rear 指向 0x23fe28 。 br>现在,当你使用星号解除引用时,你可以访问内存的那部分,你可以想象一个如上所示的容器,或者你可以想象你打开带有星号 *



当你通过执行 front ++ ++ front ,它会递增到下一个内存地址位置。同时 rear - - 后减少到下一个地址位置。
它不会更改地址,但是当您取消引用时,您可以访问该内容。



下面您会看到最后已交换,但地址保持不变

  | 0x23fde8 | 0x23fdf0 | 0x23fdf8 | 0x23fe00 | 0x23fe08 | 0x23fe10 | 0x23fe18 | 0x23fe20 | 0x23fe28 | 
。---------。---------。---------。--------- .----- ----- ---------。---------。--------- .---------。
| A | A | b | Y | L | O | N | I | B |
'---------'---------'---------'---------'----- ----' ---------'---------'---------'---------'
^ ^
前后

我希望这可以帮助您了解 *前


I have a function that takes a string,and reverses it, using front and rear pointers. Now this is the program written below. what I do not understand is the while loop. When it assigns *front = *rear , and *rear = temp //which is *front. how do we still increment and decrement front and rear respectively. But we switched them didn't we ? isn't front in the rear and rear in the front now ? or is it because it's pointers ? can someone explain this please to me ?

Program:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

void reverse(char* s);
int main()
{
    char aString[] = "babylonia";

    cout << " please enter the string " << endl;

    reverse(aString);

    cout << aString << endl;


}

void reverse(char* s)
{
    char *front, *rear, temp;
    front = s;
    rear = s+strlen(s) - 1;
    while( front < rear)
    {
        temp = *front;
        *front = *rear;
        *rear = temp;
        front++;
        rear--;
    }

}

解决方案

front = s // which is the same as front = &s[0], points to the address of the first character in the array.
And rear, which points to the address of the last character in the array.

(These are just example addresses)
|0x23fde8 | 0x23fdf0| 0x23fdf8| 0x23fe00| 0x23fe08| 0x23fe10| 0x23fe18| 0x23fe20| 0x23fe28|
.---------.---------.---------.---------.---------.---------.---------.---------.---------.
|    B    |    A    |    B    |    Y    |    L    |    O    |    N    |    I    |    A    |
'---------'---------'---------'---------'---------'---------'---------'---------'---------'
     ^                                                                               ^

At first, front points to an address for example 0x23fde8 and rear points to 0x23fe28.
Now, when you dereference it with the asterisk you can access that part of the memory, you can imagine a container as illustrated above or you can imagine that you open the container with the asterisk *

When you increment front by doing front++ or ++front, it increments to the next memory address location. At the same time asrear-- or --rear decrements to next address location. It does not change the address, but when you dereference it you can access the content.

Below you see that the first and the last have been swapped, but the addresses remain the same.

|0x23fde8 | 0x23fdf0| 0x23fdf8| 0x23fe00| 0x23fe08| 0x23fe10| 0x23fe18| 0x23fe20| 0x23fe28|
.---------.---------.---------.---------.---------.---------.---------.---------.---------.
|    A    |    A    |    B    |    Y    |    L    |    O    |    N    |    I    |    B    |
'---------'---------'---------'---------'---------'---------'---------'---------'---------'
               ^                                                           ^
             front                                                        rear

I hope that this helps you to understand the difference between *front and front

这篇关于如何使用指针反转字符串工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆