如何使用整数作为索引反转 c 中的字符串?不使用 strlen() 或其他字符串库

问题描述

我想使用函数反转字符串。这是我写的程序:

#include <stdio.h>

void reverse_string(char array[50]) {
    int lastindex,j;
    int i;
    char temp;
    for (j = 0; j <= 49; j++) {
        if (array[j] == '\0') {  \\access the index for null character
            break;
        }
        if (j != 0) {                                             
            lastindex = j - 1;  \\get last index of character string
        } else {
            lastindex = 0;
        }
    
        for (i = 0; i <= lastindex; i++) {
            temp = array[i];
            array[i] = array[lastindex];
            array[lastindex] = temp;
            lastindex--;
        }
        array[i + 1] = '\0';
    }
}

int main() {
    char myarray[500] = "Meglovania";
    reverse_string(myarray);
    printf("%s",myarray);
    return 0;
}

但是,我得到了错误输出Me

我想知道如何在不显式使用指针的情况下反转字符串。

解决方法

尝试注释掉额外的 i++。并且因为 i 最终位于字符串中间附近的某个位置,所以这不是您想要删除空字符的地方。请记住,您传递给函数的字符串已经以空字符结尾。

此外,如果 i == lastindex 则不需要交换。

    //for(i=0;i<=lastindex;i++){
      for(i=0;i<lastindex;i++){
        temp = array[i];
        array[i]= array[lastindex];
        array[lastindex]=temp;
        //i++;
        lastindex--;
    }
    //array[i+1]='\0';

正如@r3mainer 指出的那样,您的代码格式很糟糕。你在另一个里面有一个循环。我不得不将整个内容粘贴到我的 IDE 中并构建它,以解决剩余的问题。这有效:

#include<stdio.h>

void reverse_string(char array[50])
{   int lastindex,j;
    int i;
    char temp;
    
    for(j = 0;j<=49;j++){
        
        if(array[j]=='\0'){
            break;
        }
    }
    
    if(j!=0){                                             
        lastindex = j-1;

    }   
    else{
        lastindex = 0;
    }
    
    for(i=0;i<lastindex;i++){
        temp = array[i];
        array[i]= array[lastindex];
        array[lastindex]=temp;
        lastindex--;
    }
    //array[i+1]='\0'; 
}

int main()

{
    char myarray[500]="Meglovania";
    reverse_string(myarray);
    printf("%s",myarray);
    return 0;

}

这对眼睛来说更容易:

#include<stdio.h>

int string_length(char *array)
{
    int counter = 0; // Doubles as an index.

    while('\0' != array[counter]) counter++;

    return counter;
}

void reverse_string(char array[50])
{
    int i;
    char temp;
    int lastIndex = string_length(array) - 1;
        
    for(i=0;i<lastIndex;i++){
        temp = array[i];
        array[i]= array[lastIndex];
        array[lastIndex]=temp;
        lastIndex--;
    }
}

int main()

{
    char myarray[50]="Meglovania";
    reverse_string(myarray);
    printf("%s",myarray);
    return 0;

}

您在反向代码中嵌入了字符串长度代码,这是不好的做法。一个函数通常应该只做一件事,并在需要其他值时调用其他函数,所以我创建了一个 string_length 函数,最初使用您的代码版本,但我无法遵守它,所以很干净也起来。

我希望你从这个练习中学到了一些东西。代码格式非常重要。每次在代码中输入左大括号 '{' 时,立即添加右大括号 },然后在它们之间插入您需要的任何代码。

,

要使用索引变量反转字符串,您可以使用从 0 开始的索引和从字符串末尾开始的另一个索引,在这些位置交换字符并增加第一个索引并减少第二个索引,迭代虽然这些位置并不重合。

这是一个简单的实现:

#include <stdio.h>

char *reverse_string(char *str) {
    int i = 0,j = 0;
    while (str[j] != '\0') {
         j++;
    }
    while (i <-- j) {
        char c = str[i];
        str[i] = str[j];
        str[j] = c;
        i++;
    }
    return str;
}

int main() {
    char myarray[] = "Meglovania";
    printf("%s\n",reverse_string(myarray));
    return 0;
}
,
char *reverse(char *str)
{
    char *end = str,*wrk = str;

    if(str && *str)
    {
        while(*(end + 1)) end++;
        while(end > wrk) 
        {
            char tmp = *end; 
            *end-- = *wrk; 
            *wrk++ = tmp;
        }
    }
    return str;
}

int main(void)
{
    printf("%s\n",reverse((char[]){"1234567890"}));
}
,

应该这样做

#include <stdio.h> // printf
#include <string.h> // strlen

void reverse_string(char string[],size_t len) {
    if (len < 1) return;
    size_t front = 0,back = len - 1;
    char temp;
    while (front < back) {
        temp = string[front];
        string[front++] = string[back];
        string[back--] = temp;
    }
}

int main() {
    char string[500] = "sample string";
    reverse_string(string,strlen(string));
    printf("%s\n",string);
    return 1;
}