编程以在C中反转字符串而不声明char []

时间:2015-06-23 09:32:59

标签: c string pointers 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)!='\0')
    {
        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;
}

6 个答案:

答案 0 :(得分:2)

删除name=name+count;,因为先前循环中的name++移动name指向'\0'字符的指针;

#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)!='\0')
    {
            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);
}

答案 1 :(得分:2)

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

答案 2 :(得分:2)

重要提示: 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) != '\0' && (*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 '\0'
        printf("%c", (*name));
        name = name - 1;
    }
    return 0;
}

我得到了以下输出:

  

输入字符串rakeb

     

你输入了rakeb

     

bekar

答案 3 :(得分:0)

最简单的方法?只需用它们的句法等价物替换它们:

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;
  }
}

答案 4 :(得分:0)

尝试这样不仅可以打印,还可以反转字符串并将其存储在名称中。

#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) != '\0' && (*backup1) != '\r') {
    backup1++;
}

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

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

答案 5 :(得分:0)

请发布干净编译的代码

当前发布的代码缺少必需/使用的头文件

以下代码

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)!='\0')
    {
        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