求c语言代码~从键盘输入一个字符串,并在该串中最大元素后面插入字符串“ab”

跪求~~OrZ... 跪求~~OrZ 展开
 我来答
qizhi0119
2007-12-01 · TA获得超过357个赞
知道小有建树答主
回答量:510
采纳率:0%
帮助的人:272万
展开全部
#include <stdio.h>
#include <string>
main()
{
char str[80];
char *p1,*p2,*max,*p_e;
char temp;
int n;
puts("input a string:");
gets(str);//输入串
p1=str;
max=p1;
temp=*p1;//将临时最大值放在temp
p_e=str+strlen(str)-1;//尾指针
n=strlen(str);
while(*p1)//找最大值,将max指针指向最大值
{
if(*p1>temp)
{
max=p1;
temp=*p1;
}
p1++;
}
if(max==p_e)//如果最大值在尾部
{
p_e++;
*p_e='a';
p_e++;
*p_e='b';
p_e++;
*p_e='\0';
}
else//最大值不在尾部,要先移前面的向后移二位
{
*(p_e+3)='\0';
p2=p_e+2;
while(p_e!=max)
{
*p2=*p_e;
p2--;
p_e--;
}
p_e++;
*p_e='a';
p_e++;
*p_e='b';
}
printf("%s",str);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
义白萱BT
2007-12-02 · 超过23用户采纳过TA的回答
知道答主
回答量:86
采纳率:0%
帮助的人:0
展开全部
老兄,也太长点了吧。汗~~~!

#include "stdio.h"
#include "string.h"
define SIZE 80
void main()
{
int i=0;
char str1[SIZE],str2[SIZE],max;
printf("\nplease input a string:\n");
gets(str1);
printf("\nbefore cut in:\n");
printf(str1)
max=i;
for(i=0;i<=strlen(str1)-1;i++)
{
if(str1[i]<str1[i+1])
max=i+1;
}
for(i=0;i<=max;i++)
str2[i]=str1[i];
for(i=max+1;i<80;i++)
str2[i+2]=str1[i];

str2[max+1]='a';
str2[max+2]='b';
strcpy(str1,str2);
printf("\nAfter cut in:\n");
printf(str1);
getch();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
帐号已注销
2007-12-01 · TA获得超过7524个赞
知道小有建树答主
回答量:1463
采纳率:0%
帮助的人:1457万
展开全部
占位
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
楼骞2d
2007-12-01 · 超过14用户采纳过TA的回答
知道答主
回答量:82
采纳率:0%
帮助的人:54.1万
展开全部
char *c;
scanf("%s",c);
len=strlen(c);
//下面算法就自己搞定
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式