我正在尝试制作一个程序,它接受“Hello”的输入并通过反转字符的顺序输出“olleH”。但是我不断遇到段错误,我不明白为什么
#include <stdio.h>
#include<string.h>
int main()
{
int i;
int size;
char s[100],a[100];
printf("Enter the word you want to get reversed: ");
scanf("%s",s);
while(s[i]!='\0')
{
a[i]=s[i];
i++;
}
size=sizeof(s);
while(i<sizeof(s))
{
s[i]=a[size];
}
printf("The reversed string is : %s",s);
}
最佳答案
另一种简单的反转字符串的方法。
试试这个:
while(s[++i]!='\0'); // find the size of string
while(i>=0)
a[j++] = s[--i]; // reverse the string
a[j]='\0';
printf("The reversed string is : %s",a);
关于C 编程、分段故障核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39329077/