我想反转输入的字符串,我使用指向最后一个字符的指针并在指针地址减少的同时打印字符串。但是,我无法获得整个字符串。谁能告诉我哪里出了问题?
//Reverse a charater string
#include<stdio.h>
int main()
{
char a[50];
int l;
int *p;
printf("Please input a character string:\n");
scanf("%s",a);
l = strlen(a);
for (p = &a[l-1]; p >= a; p--)
{
printf("%c",*p);
}
printf("\n");
return 0;
}
Output:
Please input a character string:
asdfasdfasdf
fff
最佳答案
您已将 p
声明为整型指针。它应该是一个字符指针。
int *p;
应该是
char *p;
关于c - 反转输入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24967171/