帮忙写两个C语言程序

来源:百度知道 编辑:UC知道 时间:2024/05/28 13:29:19
1.Write a function int IsPalindrome(char s[]) that takes a string as an argument and returns the int value 1 if the string is a palindrome and returns 0 otherwise.

2.Write a function void sort(int a[], int n) that sorts an integer array from lowest to highest by applying either selection sort or insertion sort method.
注意是两个程序,麻烦标明一下,希望能给出解释。谢谢。

呵呵,今天刚看到你增加的要求,下面是增加注释后的程序,都是可单独运行的:)

//<第1题>
#include<stdio.h>
#define N 50 //字符串的最大长度,可以根据需要修改

int IsPalindrome(char s[]){ //判断一个字符串是否是回文.回文是如abcdcba的对称形式的字符串
char* p,*q;
p=q=&s[0];
while(*q!='\0') //使q指向最后一个字符
q++;
q--;
while(p<=q){ //循环开始时,p指向字符串第一个字符,q指向最后一个字符
if(*p!=*q)
return 0;
p++;q--;
}
return 1;
}
void main(){ //主调函数
char s[N];
int flag;
printf("输入字符串:\n"); //输入字符串
scanf("%s",&s);
flag=IsPalindrome(s);
if(flag)
printf("%s是回文\n",s);
else printf("%s不是回文\n",s);
}

//<第2题>
#include<stdio.h>
#define M 10 //数组的最大长度,可根据需要修改
/*选择排序基本思想:每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。*/
void sort(int s[],int length){ //选择排序
int min,k;
for(int i=0;i<length;i++){
min=s[