5

Hi i trying to implement a reverse array code but it doesnt seem to work and im really not sure why. The For loop just doesnt seem to work. I dont know why because the logic seems pretty right to me.

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

void reverse(char, int);

int main()
{
    char a[100];
    gets(a);

    reverse(a, strlen(a)-1);

    printf("%s\n",a);
    getchar();
    getchar();
    getchar();
    return 0;
}

void reverse(char ar[], int n)
{
    char c;
    int i = 0;
    printf("n = %d" , n);
    for ( i = 0; i >= n ; i++){
        c = ar[i];
        ar[i] = ar[n];
        ar[n] = c;
        printf("Processed");
        n--;}

}


/*
if (begin >= n)
return;

c          = *(x+begin);
*(x+begin) = *(x+n);
*(x+n)   = c;
offs = x++;
printf("Begin = %d   ,  n = %d, offs = %p  \n", begin, n, offs);
reverse(x, ++begin, --n); */
4

5 回答 5

5
void reverse(char, int);  //declaration wrong

void reverse(char[], int);
                 ^^^ 

你的循环

for ( i = 0; i >= n ; i++) // this fails i=0, n=some size

应该

for ( i = 0; i <= n ; i++)

避免使用gets()usefgets()代替。

于 2013-10-11T03:24:57.213 回答
1

for 循环条件应该是 'i < n'。和原型声明应该匹配。

于 2013-10-11T03:32:25.107 回答
0

for 循环条件应该是 'i < n'。和原型声明应该匹配。

“int n”是数组的大小。所以“i<=n”会使相同的数组从端到中反转,再从中到上反转。所以结果与数组相同。将“n”设为数组大小的一半。

于 2014-01-30T03:32:25.057 回答
0

我认为更好地使用宏来完成这项任务。在下面的代码中,它是一个宏SWAP


内容文件 main.c

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

// swap values with respect a type it
#ifndef SWAP
    #define SWAP(type, a, b) \
    { \
        type temp = a; \
        a = b; \
        b = temp; \
    }
#endif


/*
    Print an array integer items
 */
void
printIntArray(int array[], size_t length) {
    char ending_charapter[] = ", ";
    putchar('[');
    for (size_t i = 0; i < length; ++i) {
        printf("%d", array[i]);
        if (i < length - 1) {
            printf("%s", ending_charapter);
        }
    }
    puts("]");
}


/*
    Print an array float items
 */
void
printFloatArray(float array[], size_t length) {
    char ending_charapter[] = ", ";
    putchar('[');
    for (size_t i = 0; i < length; ++i) {
        printf("%f", array[i]);
        if (i < length - 1) {
            printf("%s", ending_charapter);
        }
    }
    puts("]");
}


/*
    Reverse an integer array in place
 */
static int
reverseIntArray(int *array, const size_t length) {
    for (int i = 0; i < length / 2; ++i) {
        SWAP(int, array[i], array[length - i - 1]);
    }
    return 0;
}


/*
    Reverse an float array in place
 */
static int
reverseFloatArray(float *array, const size_t length) {
    for (int i = 0; i < length / 2; ++i) {
        SWAP(float, array[i], array[length - i - 1]);
    }
    return 0;
}


/*
    Reverse an string
 */
static int
reverseString(char string[]) {
    size_t str_len = strlen(string);
    for (int i = 0; i < str_len / 2; ++i) {
        SWAP(char, string[i], string[str_len - i - 1]);
    }
    return 0;
}


int
main (const int argc, const char *argv[])
{
    puts("An example reverse for a int array");
    int arrInt[4] = {1, -2, 3, -4};
    printIntArray(arrInt, 4);
    reverseIntArray(arrInt, 4);
    printIntArray(arrInt, 4);

    puts("An example reverse for a float array");
    float arrFloat[4] = {0.1, -2.12, 1.3, -4.2};
    printFloatArray(arrFloat, 4);
    reverseFloatArray(arrFloat, 4);
    printFloatArray(arrFloat, 4);

    puts("An example reverse for a string");
    char str[] = "Simple text";
    puts(str);
    reverseString(str);
    puts(str);

    return 0;
}

编译为:

gcc std=c11 -I /usr/include/ -o main main.c

结果:

An example reverse for a int array
[1, -2, 3, -4]
[-4, 3, -2, 1]
An example reverse for a float array
[0.100000, -2.120000, 1.300000, -4.200000]
[-4.200000, 1.300000, -2.120000, 0.100000]
An example reverse for a string
Simple text
txet elpmiS

笔记:

  1. 刚工作
  2. Workint 与任何内置类型
  3. 测试不佳,仅使用 GCC 编译器
  4. 基于

    4.1定义一个预处理宏swap(t, x, y)

    4.2原地反转数组

    4.3 本题答案


测试环境

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 8.6 (jessie)
Release:    8.6
Codename:   jessie
$ uname -a
Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux
$ gcc --version
gcc (Debian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
于 2017-02-06T08:29:33.240 回答
-1

不会撒谎,似乎您在“反向功能”中处理了太多。我个人喜欢尽可能地分解我的代码,以便更容易发现错误。

首先,您可能希望将交换过程(for 循环)放在它自己的名为“swap”的函数中。您可以使用 char 指针 'a' 和 'b' 作为参数来执行此操作。

于 2020-07-22T13:40:26.670 回答