在不改变特殊字符位置的情况下反转字符串

在不改变特殊字符位置的情况下反转字符串

本文介绍了在不改变特殊字符位置的情况下反转字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>

using namespace std;

char chng(char a){
    int left,right,i=0,count=0;
    char temp,output;
    left=0;
    while(a[i]!='\0'){

        count++;
        i++;

    }
    right=count-1;
while(left<right){ if(!(a[left]="">='a'&& a[left]<='z')){
            left++;
        }

  else if(!(a[right]>='a'&& a[right]<='z')){

                right--;
                }

        else{
            temp=a[left];
            a[left]=a[right];
            a[right]=temp;

        }
    left++;
    right--;

}
    output = a;
    return output;

}

int main()
{
    char b;
    cin>>b;
    chng(b);
    return 0;
}





我的尝试:



的问题是:

char to char *

int char问题

如何传递指针字符串的东西

请解释



What I have tried:

the problem is:
char to char*
int char problem
how to pass pointer string stuffs
please explain

推荐答案

char chng(char a){

这意味着你只能将一个字节传递给方法。

但是......你马上开始使用它就好像它是一个字符数组:

Which means that you can only pass a single byte to the method.
But...you immediately start to use it as if it was an array of characters:

while(a[i]!='\0'){

这很糟糕,由于很多原因,所有这些都以基本数组定义为中心, 数组的名称是指向第一个元素的指针 的。这意味着系统正在尝试将您的单字节值用作多字节指针 - 32位系统为4个字节,64位为8个字节。无论你做什么,这都行不通。

你也试图将一个指针作为单个字符返回,这也不适合工作。



首先更改你的函数声明:

That's bad, for a lot of reasons, all of them centered around the fundamental array definition is that the name of an array is a pointer to the first element. Which mean that the system is trying to use your single byte value as a multibyte pointer - 4 bytes for a 32 bit system, 8 on a 64 bit). That isn't going to work, no matter what you do.
You are also trying to return a pointer as a single character, which isn;t goign to work either.

So start by changing your function declaration:

char* chng(char* input){

(不要使用单个字符名称,这使得以后阅读代码变得更加困难 - 所有变量的描述性名称有助于您的代码自我记录)。



然后更改 main 以获取字符串而不是用户的字符,并将其传递给函数。当你有这个工作时,写另一个函数,你传递一个字符串和两个整数,它会反转你传递的两个索引之间的所有字符。使用它来反转整个字符串以进行测试,并在main方法中打印结果。当一切正常时,开始着手保留特殊字符。



在这样的小阶段完成所有这一切意味着每次你只做一些额外的工作,但你知道它所基于的一切都在起作用。它可能看起来更慢,但是它可以获得很多解决方案,比尝试一次编写整个内容并发现某些东西不起作用快得多!





回复:

(Don't use single character names, it makes it a lot harder to read your code later - descriptive names for all your variables helps your code become self documenting).

Then change main to get a string instead of a character from your user, and pass that to the function. When you have that working, write another function which you pass a string and two integers, which reverses all the characters between the two indexes you pass. Use that to reverse the whole string for testing and print the result in your main method. When that all works, start working on leaving the special characters alone.

Doing it all in small stages like this means that each time you are only doing a little bit extra work, but that you know that everything it's based on is working. It may seem slower, but it gets to the solution a lot, lot faster than trying to write the whole thing at once and finding that something doesn't work!


Reply:

#include <iostream>

using namespace std;

char* chng(char* a){
    int left,right,i=0,count=0;
    char *temp,output;
    left=0;
    while(a[i]!='\0'){

        count++;
        i++;

    }
    right=count-1;
while(left<right){ if(!(a[left]="">='a'&& a[left]<='z')){
            left++;
        }
        else if(!(a[right]>='a'&& a[right]<='z')){

                right--;
                }
        else{
            *temp=a[left];
            a[left]=a[right];
            a[right]=*temp;

        }
    left++;
    right--;

}
    output= *a ;
    return output;

}

int main()
{
    char* b;
    cin>>b;
    cout<<chng(b);
    return 0;
}





我从哪里开始...



让我们从头开始,因为在你掌握了基本知识之前,没有其他任何东西可以工作。



Where do I start ...

Let's start at the beginning, because until you get the basics right, nothing else can be expected to work.

int main()
{
    char* b;
    cin>>b;
    cout<<chng(b);
    return 0;
}

将变量声明为 char * 不会为用户分配任何内存以输入:那么数据在哪里阅读 cin 应该去?

暂时取消对 chng 的调用,并且首先使用主要工作 - 使用 cout 将用户输入回显给他,并使其正常工作。

我很确定你知道如何,或者被告知如何:这不是你好世界所以用户输入是你以前会做的事情。停止猜测你需要做什么,并考虑一下 - 这并不复杂!

Declaring a variable as char* doesn;t allocate any memory for your user to type into: so where is the data you read with cin supposed to go?
Remove the call to chng for a moment, and just get main working first - use cout to echo the user input back to him, and get that working.
I'm pretty sure you know how, or have been told how: this isn't "hello world" so user input is something you will have done before. Stop guessing what you need to do, and think about it - this isn't complicated!


这篇关于在不改变特殊字符位置的情况下反转字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:40