无法反转字符串 [英] Not able to reverse the string

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

问题描述

#include <iostream>
#include<cstring>
using namespace std;
int count=0;
void reverseit(char st[100])
{
    char *c,*d,temp;
    for(int i=1;i<=count;i++)
    {
        c=(st+i);       //begining pointer
        d=(st+count);   //last pointer
        temp=*c;
        st[i]=*d;
        st[count]=temp;
        count--;
    }
}
int main() {
    char ch[100];
    cout<<"enter the string to reverse it";
    cin.getline(ch,100);
    cout.write(ch,100);
    count=std::strlen(ch);
    reverseit(ch);
	return 0;
}





我的尝试:



我正在尝试反驳弦乐的意思如果我还是应该通过使用指针来打印



What I have tried:

I AM TRYING TO REVERSE THE STRING MEANS IF I HAVE HELLO SO OLLEH SHOULD BE PRINTED BY USE OF POINTERS

推荐答案

嗯......为什么这样做?你从索引1开始,而不是零?

Um...Why do you start at index 1, instead of zero?
for(int i=1;i<=count;i++)
{
    c=(st+i);       //begining pointer



即使这是正确的,为什么你要将每个角色交换两次?

假设计数为2,你会绕圈两次。所以你要交换索引0和1,然后交换索引1和0 - 这将它们重新放回......

实际上,我甚至不会在这里使用指针 - 你有一个数组,所以只需设置两个索引(开始和结束)并开始交换 - 但只有一半的数组。



如果你用调试器查看它,它会非常明显 - 习惯使用它,它将为你节省大量的时间和头发拉动......


And even if that was right, why are you swapping every character twice?
Assume the count is 2 and you would go round your loop twice. So you'd swap index 0 and 1, then swap indexes 1 and 0 - which puts them back again...
In fact, I wouldn't even use pointers here - you have an array, so just set up two indexes (start and end) and start swapping - but only for half the array.

If you'd looked at this with the debugger, it would have been extremely obvious - get used to using it, it will save you a huge amount of time and hair pulling in the future...


使用C / C ++数组索引从零开始。你必须改变你的循环,从零开始迭代直到 count-1 并在访问最后一个字符之前递减计数。 您还在更改循环内的 count 值,但将其用作结束条件。因此,循环只处理半个字符串。



一个可能的解决方案可能是:

With C/C++ array indexes start at zero. Sou you must change your loop to iterate from zero until count-1 and decrement count before accessing the last character. You are also changing the count value inside your loop but using it as end condition. As a result the loop would process only the half string.

A possible solution might be:
for (int i = 0; i < count; i++)
{
    count--;
    temp = st[i];
    st[i] = st[count];
    st[count] = temp;
}


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



您的代码很复杂且有问题。使用调试器查看你的代码到底在做什么。

如果你想以相反的顺序打印,你应该在打印前反转。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Your code is way complicated and buggy. Use the debugger to see what your code is really doing.
If you want to print in reverse order, you should reverse before printing.


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

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