微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

这个字符反转函数有什么问题?

如何解决这个字符反转函数有什么问题?

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

#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");

}

解决方法

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

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 ) );
,
for (int i = 0; A[i] != NULL; i++) {
    temp[count]=A[i];
    count--;
}

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

,

好的,一些事情。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。