简体   繁体   English

如何仅使用指针反转字符串?

[英]How to reverse a string with pointers only?

I'm trying to reverse a string, but it just stays the same.我试图反转一个字符串,但它只是保持不变。 I don't use any modules except <string.h> and <stdio.h> .除了<string.h><stdio.h>之外,我不使用任何模块。

void rev(s){
    char i, temp;
    char *sf = s;
    char ri = strlen((s) - 1);
    char *sl = &s[ri];
    for (i = 0; i < ri; i++){
        if (*sf != *sl){
            temp = *sf++;
            s[i] = *sl--; //
            s[ri--] = temp; //those two seems to be getting new characters, but it won't
        }
        else {
            ri--;
            sf++;
            sl--;
        }
    }
    printf("%s", s);
}

The function will not compile at least because the parameter does not have a type specifier.该函数将不会编译,至少因为参数没有类型说明符。

void rev(s){

The type char has a little range of acceptable values. char类型的可接受值范围很小。 So you shall not use it for calculating the length of a string.所以你不应该用它来计算字符串的长度。

The call of strlen in this declaration本声明中对strlen的调用

char ri = strlen((s) - 1);

invokes undefined behavior.调用未定义的行为。 It seems you mean看来你的意思

char ri = strlen(s) - 1; 

that also can invoke undefined behavior for an empty string.这也可以为空字符串调用未定义的行为。

This loop这个循环

for (i = 0; i < ri; i++){

does not use pointers.不使用指针。

The function can be defined the following way as it is shown in the demonsytrative program below.该函数可以通过以下方式定义,如下面的演示程序所示。

#include <stdio.h>
#include <string.h>

char * reverse( char *s )
{
    if ( *s )
    {
        for ( char *first = s, *last = s + strlen( s ); first < --last; ++first )
        {
            char c = *first;
            *first = *last;
            *last = c;
        }
    }
    
    return s;
}

int main( void ) 
{
    char s1[] = "1";
    char s2[] = "12";
    char s3[] = "123";
    
    puts( reverse( s1 ) );
    puts( reverse( s2 ) );
    puts( reverse( s3 ) );
}   

The program output is程序输出是

1
21
321

Your code has plenty of issues (bugs 🐛):你的代码有很多问题(错误🐛):

char ri = strlen((s) - 1); has to be size_t ri = strlen((s)) - 1;必须是size_t ri = strlen((s)) - 1;

Other code is very hard to analyze as you use not self explanatory variable names.其他代码很难分析,因为您使用的不是自解释变量名。

Here you have a bit more simple code and much easier to analyse.在这里你有更简单的代码,更容易分析。

char *reverseString(char *str)
{
    char *wrk = str, *end;
    if(str && *str)
    {
        end = str + strlen(str) - 1;
        while(end > wrk)
        {
            char temp = *wrk;
            *wrk++ = *end;
            *end-- = temp;
        }
    }
    return str;
}


int main(void)
{
    char str[] = "1234567890";
    printf("reversed: %s\n", reverseString(str));
}

A simple solution:一个简单的解决方案:

char *sl = sf;
while (*sl != 0)
    ++ sl;
-- sl; 
while (sf < sl)
{
    char c = *sf;
    *sf = *sl;
    *sl = c;

    ++sf, --sl;
}

Find the end of the string by skipping all characters until you find the NUL (zero) character.通过跳过所有字符直到找到 NUL(零)字符来查找字符串的结尾。
Then step back one character (decrement sl ) so that you have pointers to the first and the last character of the string.然后后退一个字符(递减sl ),以便您拥有指向字符串的第一个和最后一个字符的指针。

Then walk both pointers towards one another and swap characters until the pointers meet or cross.然后将两个指针朝向彼此并交换字符,直到指针相交或交叉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM