c语言作业:从键盘输入一个字符串a,找出其中的最大元素和最小元素 从键盘输入一个字符串a,并在a串中的最大元素后面插入字符串b
c\u8bed\u8a00\u4ece\u952e\u76d8\u8f93\u5165\u4e24\u4e2a\u5b57\u7b26\u4e32a\u548cb\uff0c\u5e76\u5728a\u4e32\u4e2d\u7684\u6700\u5927\u5143\u7d20\u540e\u8fb9\u63d2\u5165\u5b57\u7b26\u4e32b\uff01\u6709\u56fe \u5c31\u7a0b\u5e8f#include #include #include const char *max(const char *x);int main(void){ char string1[1024] = {0}; //\u5b57\u7b26\u4e321; char string2[1024] = {0}; //\u5b57\u7b26\u4e322; printf("Input string1:"); gets(string1); //\u8f93\u5165\u4e321; printf("Input string2:"); gets(string2); //\u8f93\u5165\u4e322; char *newstr = (char*)calloc(strlen(string1) + strlen(string2) + 1, sizeof(char)); //\u521b\u5efa\u4e00\u4e2a\u521a\u597d\u5bb9\u7eb3\u4e24\u4e2a\u4e32\u7684\u65b0\u4e32; if(!newstr) //\u68c0\u67e5\u662f\u5426\u521b\u5efa\u6210\u529f; { printf("\u5185\u5b58\u4e0d\u8db3,\u7a0b\u5e8f\u7ed3\u675f!\n"); exit(1); } const char *m = max(string1); //\u8c03\u7528max\u51fd\u6570\u53d6\u4e321\u5185\u6700\u5927\u5143\u7d20\u7684\u5730\u5740; int i = 0; for(; &string1[i] != m; ++i) //\u4e321\u6700\u5927\u5143\u7d20\u4e4b\u524d\u7684\u5185\u5bb9\u653e\u5165\u65b0\u4e32; newstr[i] = string1[i]; newstr[i] = string1[i]; //\u4e321\u6700\u5927\u5143\u7d20\u653e\u5165\u65b0\u4e32; strcat(newstr, string2); //\u4e322\u63a5\u5165\u65b0\u4e32; strcat(newstr, m + 1); //\u4e321\u6700\u5927\u5143\u7d20\u4e4b\u540e\u7684\u5185\u5bb9\u63a5\u5165\u65b0\u4e32; printf("The new string is %s\n", newstr); //\u8f93\u51fa\u65b0\u4e32; free(newstr); //\u91ca\u653e\u5185\u5b58; newstr = NULL; //\u653e\u7a7a\u6307\u9488; return 0;}const char *max(const char *x) //\u8fd4\u56de\u4e32\u4e2d\u6700\u5927\u5143\u7d20\u5730\u5740; { const char *m = &x[0]; //\u6307\u5411\u7b2c\u4e00\u4e2a\u5143\u7d20; for(int i = 1; x[i] != '\0'; ++i) //\u5faa\u73af\u5230'\0'; if(x[i] > *m) //\u82e5\u6bd4m\u5f53\u524d\u6307\u5411\u7684\u5143\u7d20\u5927, m = &x[i]; //\u8ba9m\u6307\u5411\u8be5\u5143\u7d20\u5730\u5740; return m; //\u8fd4\u56de\u6700\u5927\u5143\u7d20\u5730\u5740; }
\u4ee3\u7801\uff1a
#include
#include
#define N 356
void main(){
char a[N]={\u2019\0\u2019},b[N]={\u2019\0\u2019},ch,max;
int i,j,k,c;
/\u8f93\u5165\u5b57\u7b26\u4e32/
printf(\u201c\u8bf7\u8f93\u5165\u5b57\u7b26\u4e32\u201d);
gets(a);
/\u8f93\u5165ch\u7684\u503c/
printf(\u201cInput ch:\u201d);
scanf("%c",&ch);
/\u6c42\u51fa\u6700\u5927\u5b57\u7b26/
i=1;
max=a[0];
while(a[i]!=\u2019\0\u2019){
if(a[i]>max)
max=a[i];
i++;}
printf("%c\n",max);
/\u6c42\u51fa\u6700\u5927\u5b57\u7b26\u4f4d\u7f6e/
i=0;
while(a[i]!=\u2019\0\u2019){
if(a[i]==max)
break;
i++;
}
i++;
c=i;
/\u628a\u6700\u5927\u7684\u5b57\u7b26\u540e\u9762\u7684\u5b57\u7b26\u653e\u5728\u5b57\u7b26\u4e32b\u91cc/
j=0;
while(a[i]!=0){
b[j]=a[i];
j++;
i++;
}
/\u628ach\u8d4b\u503c\u7ed9\u6700\u5927\u503c\u540e\u9762\u7684\u4f4d\u7f6e/
a[c]=ch;
c++;
/\u628a\u5b57\u7b26\u4e32b\u7684\u6570\u8fd4\u56de\u7ed9a/
i=0;
while(b[i]!=\u2019\0\u2019){
a[c]=b[i];
c++;
i++;}
/\u8f93\u51fa\u7ed3\u679c/
printf("%s\n",a);
}
\u6269\u5c55\u8d44\u6599\uff1ainclude\u7528\u6cd5\uff1a
#include\u547d\u4ee4\u9884\u5904\u7406\u547d\u4ee4\u7684\u4e00\u79cd\uff0c\u9884\u5904\u7406\u547d\u4ee4\u53ef\u4ee5\u5c06\u522b\u7684\u6e90\u4ee3\u7801\u5185\u5bb9\u63d2\u5165\u5230\u6240\u6307\u5b9a\u7684\u4f4d\u7f6e\uff1b\u53ef\u4ee5\u6807\u8bc6\u51fa\u53ea\u6709\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u624d\u4f1a\u88ab\u7f16\u8bd1\u7684\u67d0\u4e00\u6bb5\u7a0b\u5e8f\u4ee3\u7801\uff1b\u53ef\u4ee5\u5b9a\u4e49\u7c7b\u4f3c\u6807\u8bc6\u7b26\u529f\u80fd\u7684\u5b8f\uff0c\u5728\u7f16\u8bd1\u65f6\uff0c\u9884\u5904\u7406\u5668\u4f1a\u7528\u522b\u7684\u6587\u672c\u53d6\u4ee3\u8be5\u5b8f\u3002
\u63d2\u5165\u5934\u6587\u4ef6\u7684\u5185\u5bb9
#include\u547d\u4ee4\u544a\u8bc9\u9884\u5904\u7406\u5668\u5c06\u6307\u5b9a\u5934\u6587\u4ef6\u7684\u5185\u5bb9\u63d2\u5165\u5230\u9884\u5904\u7406\u5668\u547d\u4ee4\u7684\u76f8\u5e94\u4f4d\u7f6e\u3002\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6307\u5b9a\u63d2\u5165\u5934\u6587\u4ef6\uff1a
1\u3001#include;
2\u3001#include"\u6587\u4ef6\u540d"
#include<stdio.h>
int main()
{
char str[200];
scanf("%s",str);
char min,max;
int minid,maxid;
int i=0;
min=max=str[0];
maxid=minid=0;
while(str[i]!='\0')
{
if (str[i]>max)
{
max=str[i];
}
if (str[i]<min)
{
min=str[i];
minid=i;
}
i++;
}
i=0;
while(str[i]!='\0')
{
if (str[i]==max)
{
printf("最大元素是%c 位置%d
",max,i);
}
if (str[i]==min)
{
printf("最小元素是%c 位置%d
",min,i);
}
i++;
}
return 0;
}
绛旓細if(c>='a"||c<='z')cout<<"alphabetic character";else cout<<"other character";澶ц嚧灏辫繖鏍峰惂
绛旓細include<stdio.h>int main(){char a;scanf("%c",&a);printf("%c",a+1);return 0;}
绛旓細include<stdio.h> int j=0,k=0,l=0;int main(){char a[100],*p;void can(char *p);printf("杈撳叆涓涓瓧绗︿覆");p=a;gets(a);can(p);printf("澶у啓瀛楁瘝%d涓猏n",j);printf("灏忓啓瀛楁瘝%d涓猏n",k);printf("鏁板瓧%d涓猏n",l);return 0;} void can(char *p){for(;*p;p++)if((...
绛旓細int main(){ char str[100];long long an[100];int cnt=0,i=0,f=0;printf("Please input a string:");gets(str);while(str[i]){ if(str[i]>='0'&&str[i]<='9'){ if(f==1){ an[cnt]=an[cnt]*10+str[i]-'0';} if(f==0){ cnt++;f=1;an[cnt]=str[i]-'0'...
绛旓細voidmain(){ charch;inta;ch=getchar();if(ch>='a'&&ch<='z')ch='1';if(ch>='A'&&ch<='Z')ch='2';if(ch=='')ch='3';switch(ch){ case'1':printf("xiao\n");break;case'2':printf("da\n");break;case'3':printf("绌烘牸\n");break;default:printf("qita");} } ...
绛旓細1銆侀鍏堥渶瑕佹墦寮缂栫▼杞欢銆2銆杈撳叆浠ヤ笅绋嬪簭锛#include <stdio.h>int main(){ char str[40]; scanf("%s",&str); printf("%s",str); return 0;}銆3銆佺劧鍚庢寜F5杩愯绋嬪簭銆4銆佺劧鍚庤緭鍏ユ兂瑕佽緭鍏ョ殑瀛楃涓銆5銆佹寜鍥炶溅閿紝灏变細寮瑰嚭浣犺緭鍏ョ殑瀛楃涓层6銆佹敞鎰忓瓧绗︿覆鐨勫ぇ灏忥紝鏀瑰彉涓嫭鍙蜂腑鐨勬暟瀛楁敼鍙樿緭鍏...
绛旓細绋嬪簭婧愮爜濡備笅锛歞efine _CRT_SECURE_NO_WARNINGS//VS鐜涓嬮渶瑕侊紝VC涓嶉渶瑕 include<stdio.h> void main(){ char a;//瀹氫箟涓涓瓧绗 printf("璇杈撳叆涓涓瓧绗︼細");//鏂囧瓧鎻愮ず scanf("%c", &a);//杈撳叆涓涓瓧绗 printf("鍏跺悗缁瓧绗︿负锛");//鏂囧瓧鎻愮ず printf("%c\n", a + 1);//杈撳嚭鍏...
绛旓細include<stdio.h>int main(){char str[200];scanf("%s",str);char min,max;int minid,maxid;int i=0;min=max=str[0];maxid=minid=0;while(str[i]!='\0'){if (str[i]>max){max=str[i];}if (str[i]<min){min=str[i];minid=i;}i++;}i=0;while(str[i]!='\0'){...
绛旓細void main(){ char input[1000];int i=0,out[26]={0},j;char outstring[26]= {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};cout<<"Please input capital password:";fge...
绛旓細int countd=0,countx=0,countk=0,counts=0,countq=0;//鍒嗗埆鐢ㄦ潵瀵瑰ぇ鍐欏瓧姣嶃佸皬鍐欏瓧姣嶃佺┖鏍笺佹暟瀛椼佸叾浠栧瓧绗﹀仛璁℃暟 char s[100],*p;printf("璇杈撳叆涓涓瓧绗︿覆锛");int i=0;while((s[i]=getchar())!='\n')i++;p=&s[0];while(*p!='\n'){ if((*p>='A')&&(*p<='Z'))...