指针运用及函数调用,求高手指教!!!
编写子函数,实现将一个二维字符数组中的每一个字符串的字符按升序重新排列。要求能初始化输入字符串数组并输出。主函数只能调用一次子函数
程序代码:#include "stdio.h"
#include "stdlib.h"
main()
{
// void insort(int s[],int n);
int a[11],i;
printf("please input 10 numbers:\n");
for(i=1;i<=10;i++)
scanf("%d",&a[i]);
printf("the original order:\n");
for(i=1;i<=10;i++)
printf("%4d",a[i]);
printf("\nthe sorted numbers:\n");
insort(a,10);
for(i=1;i<=10;i++)
printf("%4d",a[i]);
printf("\n");
system("pause");
}
void insort(int s[],int n)
{
int i,j;
for(i=2;i<=n;i++)
{
s[0]=s[i];//s[0]为监视哨
for(j=i-1;s[j]>s[0];j--)
s[j+1]=s[j];
s[j+1]=s[0];//确定位置插入s[i]
}
}
程序代码:#include <stdio.h>
void sort_chars(char * s, int max_length, int cnt) {
char * p, * q, c;
for (; cnt-- > 0; s += max_length) {
for (p = s; *p; p++) {
for (q = p + 1; *q; q++) {
if (*p > *q) {
c = *p;
*p = *q;
*q = c;
}
}
}
}
}
int main() {
char s[100][100];
int n, i;
printf("How many strings will give me? (1 ~ 100) ");
while(scanf("%d", &n) == 0) {
printf("I need a number between 1 and 100 ");
while (getchar() != '\n');
}
while (getchar() != '\n');
for (i = 0; i < n; i++) {
gets(s[i]);
}
sort_chars(&s[0][0], 100, n);
printf("\nAfter sorting, the strings look like these:\n");
for (i = 0; i < n; i++) {
puts(s[i]);
}
return 0;
}