在字符串str中找出ASCⅡ码值最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动。

#include <stdio.h>
fun( char *p )
{
char max,*q; int i=0;
max=p[i];
while(p[i]!=0)
{
if(max<p[i])
{
max=p[i];
q=p+i; //这里是什么意思 还有*q是做什么的?求解。
}
i++;
}
while(q>p)
{
*q=*(q-1);
q--;
}
p[0]=max;
}
main()
{
char str[80];
printf("Enter a string: ");
gets(str);
printf("\nThe original string:");
puts(str);
fun(str);
printf("\nThe string after moving: ");
puts(str);
printf("\n\n");
}

q=p+i是使指针向后移动。其实楼主的程序中不用指针也是可以的:
fun(char *p)
{
int i=0,pos=0;//pos记录最大字符的位置
char max = p[0];
for(i=1;p[i];i++)
{
max=p[i];//保存最大字符
pos=i; //及其位置
}
for(i=pos;i>0;i--) p[i]=p[i-1];//将最大字符前的所有字符后移
p[0]=max;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-01-30
p是数组p[ ]的首地址,第i个最大,p+i就是最大的那个的地址本回答被提问者和网友采纳
第2个回答  2012-01-29
q指向最大字符在在字符串中位置
第3个回答  2012-01-30
指针变化,就是地址变化
第4个回答  2012-01-29
字符
相似回答