简体   繁体   English

在 C 中使用指针的反向数组

[英]Reverse array with using pointers in C

#include <stdio .h>
#include <stdlib .h> 
int main(){
    char text1 [N] ; 
    char reverse [N] ;
    char* txtptr = text1 ;
    char* revtxtptr = reverse ;
    int N;
    printf (”\n Enter any text here : ”) ;
    scanf(”%s”, text1); 
    while(N> 0){
        txtptr --;
        *revtxtptr = *txtptr ;
        revtxtptr++;
    }
    *revtxtptr = ’\0’;
    printf (”The reverse text is : %s \n” , reverse) ;
    return 0; 
}

I want to see here output the reverse form of the input.我想在这里看到 output 输入的反转形式。 Something like input:类似输入的东西:

CLEARLY

output: output:

YLRAELC

Could you help me to fix my fault?你能帮我改正我的错吗?

Here are corrections to your code:以下是对您的代码的更正:

  1. You have spaces before the .h> in the #include lines. #include行中的.h>之前有空格。
  2. You should limit the size of the buffers.您应该限制缓冲区的大小。
  3. N is not initialized. N 未初始化。
  4. N is not being decremented. N 没有递减。
  5. txtptr is not being placed at the end of the C string, but it is being decremented in the while loop. txtptr没有放在 C 字符串的末尾,但它在while循环中被递减。
  6. scanf is not limited to the size of the buffer(s) minus 1. scanf不限于缓冲区的大小减 1。
  7. You need to either find the size of the string using strlen or walk the string until you find '\0'.您需要使用strlen查找字符串的大小或遍历字符串直到找到 '\0'。 (forward direction instead) (改为前进方向)
  8. You use the wrong double-quotes and single quotes ("smart" quotes)您使用了错误的双引号和单引号(“智能”引号)

Here is a safe code that will reverse the input array (not in-place):这是一个将反转输入数组(不是就地)的安全代码:

#include <stdio.h>
#include <stdlib.h>

#define MAXSTR 255
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)

int main() {
  char text1[MAXSTR + 1];
  char revbuf[MAXSTR + 1];
  char* txtptr = text1;
  char* reverse = revbuf + MAXSTR;
  printf("\nEnter any text here : ");
  scanf("%" STR(MAXSTR) "s", text1);
  *reverse = '\0';
  while(*txtptr) {
    *--reverse = *txtptr++;
  }
  printf ("The reverse text is : %s \n" , reverse) ;
  return 0;
}

Here you have an example function for int array.这里有一个用于 int 数组的示例 function。

int *reverseINTarray(int *array, size_t size)
{
    int *end, *wrk = array;
    if(array && size > 1)
    {
        end = array + size - 1;
        while(end > wrk)
        {
            int tmp = *wrk;
            *wrk++ = *end;
            *end-- = tmp;
        }
    }
    return array;
}

or或者

int *reverseINTarray(const int *src, int *dest, size_t size)
{
    int *wrk = dest;
    if(src && dest && size)
    {
        wrk += size - 1;
        while(size--)
        {
            *wrk-- = *src++;
        }
    }
    return dest;
}

Your "swap two bytes" logic is... simply... wrong.您的“交换两个字节”逻辑是......只是......错误。 Does this give you any ideas?这给你任何想法吗?

char temp;
temp = *chartxtptr;
*chartxtptr = *txtptr;
*txtptr = temp;

You can't "swap" any two bytes without using a temporary to hold the byte that is about to be replaced.如果不使用临时来保存即将被替换的字节,则不能“交换”任何两个字节。

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

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