【问题标题】:what is the issue with this char reverse function?这个字符反转功能有什么问题?
【发布时间】:2021-09-10 07:26:29
【问题描述】:

所以我编写了这段代码来根据用户选项来反转其中一个名称,想法是使用另一个函数来反转并使用指针,但是在尝试了所有我能想到的代码之后,我的代码返回相同的名称而不是最好的我能做的就是把名字的第一个字母改成一个奇怪的符号。

#include <iostream>

using namespace std;

void reverse(char* A) {
    int count = 0;
    char temp[10];
    for (int i = 0; A[i] != NULL; i++)
        count++;
    for (int i = 0; A[i] != NULL; i++) {
          temp[count]=A[i];
        count--;
    }
    for (int i = 0; A[i] != NULL; i++) {
        A[i] = temp[i];
        
    }
    
}

int main(){
    int x= 0;
    int index;

    char Name_list[5][10];
    
    
    
    cout << "please enter the names of the student  " << endl;
    for (int i = 0; i < 5; i++) {
        cin >> Name_list[i];

        for (int j = 0; Name_list[i][j] != NULL; j++) {
            x++;
        }
        while (x > 10)
        {
            x = 0;
            cout << "you have entered more then the allowed number of characters per name enter another name " << endl;
            cin >> Name_list[i];
            for (int j = 0; Name_list[i][j] != NULL; j++) {
                x++;
            }
        }
        x = 0;

    }
    for (int i = 0; i < 5; i++) {
        cout << Name_list[i] << endl;
    }
    cout << "please enter the index of the name you want to reverse" << endl;
    cin >> index;
    while (index>4||index <0)
    {
        cout << "you entered incorrect index please enter a number from 0 to 4 " << endl;

    }
    reverse(Name_list[index]);
    for (int i = 0; i < 5; i++) {
        cout << Name_list[i] << endl;
    }
    
    system("pause");

}

【问题讨论】:

  • 您是否尝试过在调试器中跟踪reverse 的每一行?
  • 调试器很有帮助,但你也可以在纸上做这个。从一个 3 个字母的单词开始,为数组槽画一些框,按照你的逻辑,看看每个字母在哪里结束。
  • 这并没有解决问题,但NULL 是一个空指针常量。它不是 char 值。它可能在这段代码中运行良好,但字符串终止符的正确值为’\0’
  • @Osama,建议:不使用“char Name_list[5][10];”,可以使用“string Name_List[5]”,然后调用Name_List[i].reverse () 反转名称。

标签: c++ reverse c-strings function-definition


【解决方案1】:

对于初学者来说,这样的函数应该返回一个指向结果字符串的指针。那就是它应该被声明为

char * reverse( char *s );

注意:不要使用由大写字母组成的变量名。

int 类型可能不够大,无法存储字符串的长度。请改用 size_t 类型。

char * reverse( char *s )
{
    size_t count = 0;
    //...

完全不清楚为什么存在一个元素数等于幻数 10 的数组

 char temp[10];

要反转字符串,无需声明辅助数组。这种方法主要是错误的。

在这个for循环中

for (int i = 0; A[i] != NULL; i++)

char 类型的对象与指针NULL 进行比较。对于这种错误的比较,编译器应该发出一条消息。看来你的意思

for (int i = 0; A[i] != '\0'; i++)

无论如何,在第一个 for 循环中引入的变量 i 是多余的,因为您已经有了变量 count

由于数组 temp 的固定大小等于 10,因此第一个循环之后的两个循环都可以调用未定义的行为,即使源字符串的长度正好等于 10。

而且结果字符串不是以零结尾的。

函数可以如下所示。

char * reverse( char *s )
{
    size_t count = 0;

    while ( s[count] ) ++count;

    for ( size_t i = 0; i < count / 2; i++ )
    {
        char c = s[i];
        s[i] = s[count - i - 1];
        s[count - i - 1] = c;
    }

    return s;
}

或者使用标准函数,您可以按以下方式编写函数

#include <utility>
#include <cstring>

//...

char * reverse( char *s )
{
    for ( size_t i = 0, n = std::strlen( s ); i < n / 2; i++ )
    {
        std::swap( s[i], s[n-i-1] ); 
    }

    return s;
}

注意有标准算法std::reverse。使用它,您可以通过以下方式反转字符串

std::reverse( s, s + std::strlen( s ) );

【讨论】:

    【解决方案2】:

    好的,有几件事。

    1. 如果您想做一些字符串操作,请查看标准库。除非你在上课时这样做。
    2. 您将所有内容写到临时结束。缓冲区
    3. 您需要在字符串末尾为空字节添加一个额外的字符(我认为此实现可能允许出现段错误)

    【讨论】:

    • 我知道,但是因为我在上课,所以有一些限制
    【解决方案3】:
    for (int i = 0; A[i] != NULL; i++) {
        temp[count]=A[i];
        count--;
    }
    

    如果i 从 0 上升到 5,count 从 6 下降到 1。

    【讨论】:

    • 感谢我所要做的就是为 null 添加 2 个点,但我没有注意到它,我在查看此代码块后立即找到了解决它的方法
    猜你喜欢
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多