在不声明 char[] 的情况下在 C 中反转字符串的程序

Program to reverse a string in C without declaring a char[]

我需要反转给定的字符串并在不使用值 At[index] notation 的情况下显示它,我使用指针尝试了下面的程序,但它不会为反转字符串打印任何内容,

请帮忙!

int main()    
{
    char* name=malloc(256);
    printf("\nEnter string\n");
    scanf("%s",name);
    printf("\nYou entered%s",name);

    int i,count;
    count=0;

   //find the length
    while((*name)!='[=10=]')
    {
        count++;
        name++;
    }

    //pointer now at
    printf("\n%p",name);

    printf("\nLength is %d",count);

    name=name+count;
    //pointer now at
    printf("\n%p",name);

    for(i=0;i<(count);i++)
    {   
        printf("%c",(*name));
        name=name-1;
    }

    return 0;
}

删除 name=name+count; 因为前面循环中的 name++name 指针移动到 '[=15=]' 字符;

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

int main()
{
    char* name=malloc(256);
    printf("\nEnter string\n");
    scanf("%s",name);
    printf("\nYou entered%s",name);

    int i,count;
    count=0;

    //find the length and move name pointer
    while((*name)!='[=10=]')
    {
            count++;
            name++;
    }

    //pointer now at
    printf("\nPointer is: %p",name);

    printf("\nLength is: %d\n",count);

    for(i=1;i<=(count);i++)
    {   
        printf("%c",*(name-i));            
    }

    printf("\n");
    return 0;
}

或将最终循环更改为

for(i=0;i<(count);i++)
{   
        name--;
        printf("%c",*name);
}

删除 name=name+count; 并添加 name--;

重要提示: scanf(" %s", name); 没有对输入进行边界检查。如果有人在您的程序中输入超过 255 个字符,它可能会出现未定义的行为。

现在,你有 char array 你有 count(数组中 char 的数量),你使 name++(名称有最后一个字符偏移量)那你为什么要费心做这样的事情?

name=name+count; 

试试这个:

#include <stdio.h>

int main()

{
    char* name = malloc(256);
//  char name[256];
    printf("\nEnter string\n");
//  scanf("%s", name);
    fgets(name, 254, stdin); // carriage return and null character (256-2) 
    printf("\nYou entered %s", name);
    int i, count;
    count = 0;
//find the length
    while ((*name) != '[=11=]' && (*name) != '\r') {
        count++;
        name++;
    }
//pointer now at
//  printf("\n%p", name);

//  printf("\nLength is %d", count);

//  name = name + count;
//pointer now at
//  printf("\n%p", name);

    for (i = count; i >= 0; i--) { // starts from last '[=11=]'
        printf("%c", (*name));
        name = name - 1;
    }
    return 0;
}

我得到以下输出:

Enter string rakeb

You entered rakeb

bekar

最简单的方法?只需将它们替换为等效的句法即可:

arr[index] // is sugar for ...
arr + index

然后,不要使用两个索引来遍历,而是使用指针。使用这个你实际上可以很容易地找到解决方案:

 void nreverse(char * str) {
  char * forward = str;
  char * backward = str + strlen(str) - 1;
  while (forward < backward) {
    char temp = *forward;
    *forward = *backward;
    *backward = temp;
    ++forward;
    --backward;
  }
}

试试这个,它不仅会打印,还会反转字符串并将其存储在名称中。

#include <stdio.h>

int main()

{
char* name = malloc(256);
char *backup1 = *bakcup2 = name;
printf("\nEnter string\n");
fgets(name, 254, stdin); // carriage return and null character (256-2) 
printf("\nYou entered %s", name);
while ((*backup1) != '[=10=]' && (*backup1) != '\r') {
    backup1++;
}

backup1--; // Because here backup1 was pointing to '[=10=]' or '\r'.
while(backup1 > backup2){
   /* Swapping characters */
    char temp;
    temp = *backup1;
    *backup1 = *backup2;
    *backup2 = temp;
    backup1--;
    backup2++;
}

backup1 = name;
while(*backup1 != '[=10=]' && *backup1 != '\r') {
    printf("%c", (*backup1));
    backup1++;
}
return 0;
}

请post 干净编译的代码

当前 posted 代码缺少 required/used 头文件

以下代码

1) includes error checking
2) limits the length of the user supplied string
   to avoid a input buffer overflow
3) eliminates certain lines (commented out)
   that caused 'name' to point to the wrong location
4) incorporates '\n' at the end of the printf() format strings
   so the info will be printed rather than held 
   in the buffer for stdout
5) at the end, passes the pointer to the malloc'd memory
   to the free() function
6) corrects the loop count when printing the 
   reverse of the input string

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

#define MAX_NAME_LEN (256)

int main()
{
    char* name=NULL;
    char* temp = NULL;

    if( NULL ==(name=malloc(256)) )
    { // then malloc failed
        perror( "malloc for name[] failed");
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    temp = name;  // save ptr to malloc'd memory
    printf("\nEnter string\n");
    if( 1 != scanf("%255s", name) )
    { // then scanf failed
        perror( "scanf for name failed");
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    printf("\nYou entered: %s\n",name);

    int i,count;
    count=0;

   //find the length
    while((*name)!='[=10=]')
    {
        count++;
        name++;
    }

    //pointer now at
    printf("\nAddress of last char in name[]: %p\n",name);

    printf("\nLength is %d\n",count);

    //name=name+count;
    //pointer now at
    //printf("\n%p",name);

    for(i=0;i<=count;i++)
    {
        printf("%c",(*name));
        name--;
    }
    printf( "\n" );

    free(temp);

    return 0;
} // end function: main