C语言,编写一个程序,输入若干字符串,找出其中最长的字符串并输出。要用指针数组存放这些字符串,并要 编写程序,输入五个字符串,输出其中最长的字符串。要求使用指针...
C\u8bed\u8a00\u7a0b\u5e8f\u8bbe\u8ba1\u9898//\u7a0b\u5e8f1
#include
void main()
{
char *p[4],*ptmp,buf[256]={0};
unsigned int n,m,size;
printf("\u4ee5\u56de\u8f66\u4e3a\u7ed3\u675f\u6807\u8bb0\uff0c\u4f9d\u6b21\u8f93\u5165\u56db\u4e2a\u5b57\u7b26\u4e32\n");
for(n=0;n<4;n++)
{
printf("\u8bf7\u8f93\u5165\u7b2c%d\u4e2a:",n+1);
gets(buf);
size=strlen(buf);
p[n]=(char*)malloc(size+1);
strcpy(p[n],buf);
}
printf("\u8f93\u5165\u7ed3\u675f\uff0c\u73b0\u5728\u5f00\u59cb\u6392\u5e8f:\n");
for(n=0;n<4-1;n++)for(m=0;m<4-n-1;m++)//\u7b80\u5355\u5192\u6ce1\u6cd5
if(strcmp(p[m],p[m+1])>0){ptmp=p[m];p[m]=p[m+1];p[m+1]=ptmp;}
//\u53ea\u52a8\u4e86\u6307\u9488\u7684\u6307\u5411\uff0c\u4e0d\u6539\u53d8\u5185\u5b58\u4e2d\u4e2d\u7684\u4f4d\u7f6e
printf("\u6392\u5e8f\u540e\u7684\u6b21\u5e8f\u662f:\n");
for(n=0;n<4;n++)printf("%s\n",p[n]);
}
//\u7a0b\u5e8f2
#include
void main()
{
char buf[1024] = {0};
char *pmax=NULL, *pmin=NULL, *ptmp;
//\u6570\u7ec4\u65e0\u6cd5\u6ee1\u8db3\u4fdd\u5b58\u4efb\u610f\u884c\u7684\u9700\u6c42
//\u800c\u4e8b\u5b9e\u4e0a\u4f60\u53ea\u8981\u6700\u957f(\u6216\u6700\u77ed)\u7684\u4e32
//\u53c8\u4e2d\u95f4\u7684\u65e0\u9700\u4fdd\u5b58\uff0c\u6240\u4ee5\u53ea\u8981\u4e24\u4e2a\u6307\u9488\u4fdd\u5b58\u5185\u5bb9\u5c31\u591f\uff0c\u4e0d\u9700\u8981\u6307\u9488\u6570\u7ec4
unsigned int n=0,nmax,nmin,size;
printf("\u8f93\u5165\u82e5\u5e72\u884c\u5b57\u7b26\u4e32\uff0c\u4ee5\u56de\u8f66\u7ed3\u675f\u6bcf\u4e00\u884c\uff0c\u7a7a\u884c\u8868\u793a\u5168\u90e8\u8f93\u5165\u7ed3\u675f\n");
begin:
printf("\u8bf7\u8f93\u5165\u7b2c%d\u4e2a\u4e32:\n",n+1);
gets(buf);//\u8bb0\u5f97DOS\u4e0bTC2\u4e2d\u8fd9\u4e2a\u51fd\u6570\u4f1a\u628a\u6362\u884c\u7b26\u4e5f\u8bfb\u8fdb\u6765\u653e\u5728\u4e32\u5c3e\u7684\uff0c\u73b0\u5728\u600e\u4e48\u90fd\u6539\u6389\u4e86
if(!*buf||*buf=='\n') goto begin;
size=strlen(buf);
pmax = (char*)malloc(size+1);strcpy(pmax,buf);nmax=size;
pmin = (char*)malloc(size+1);strcpy(pmin,buf);nmin=size;
n++;
while(1)
{
printf("\u8bf7\u8f93\u5165\u7b2c%d\u4e2a\u4e32:\n",n+1);
gets(buf);
if(!*buf||*buf=='\n')break;
size=strlen(buf);
if(size>nmax)
{
free(pmax);//\u4e32\u8d8a\u6765\u8d8a\u957f\uff0c\u4e0a\u6b21\u7684\u957f\u5ea6\u4e0d\u591f\uff0c\u9700\u91ca\u653e\u4e86\u91cd\u65b0\u5206\u914d
pmax=(char*)malloc(size+1);
strcpy(pmax,buf);
nmax=size;
}
else if(size<nmin)
{
strcpy(pmin,buf);//\u65e7\u4e32\u7a7a\u95f4\u591f\uff0c\u76f4\u63a5copy
nmin=size;
}
n++;
}
printf("\n\u6700\u957f\u5728\u4e32\u662f:\n%s\n\u6700\u77ed\u7684\u4e32\u662f:\n%s",pmax,pmin);
free(pmax);
free(pmin);
}
//VC7\u4e0b\u4e24\u4e2a\u4f8b\u5b50\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u8fd0\u884c
#include
#include
int main()
{
int i = 0;
char s[80] = {""}, max[80] = {""};
printf("\u8f93\u5165\u4e94\u4e2a\u5b57\u7b26\u4e32:\n");
scanf("%s",s);
strcpy(max,s);
for(i=1; i<5; i++)
{
scanf("%s",s);
if( strcmp(max,s)<0 )
strcpy(max,s);
}
printf("\u6700\u5927\u7684\u5b57\u7b26\u4e32\u662f:%s \n",max);
return 0;
}
\u6269\u5c55\u8d44\u6599\uff1ascanf\u51fd\u6570\u6700\u4e3b\u8981\u7684\u7528\u6cd5\u662f:
scanf("\u8f93\u5165\u63a7\u5236\u7b26",\u8f93\u5165\u53c2\u6570);
\u529f\u80fd\uff1a\u5c06\u4ece\u952e\u76d8\u8f93\u5165\u7684\u5b57\u7b26\u8f6c\u5316\u4e3a\u201c\u8f93\u5165\u63a7\u5236\u7b26\u201d\u6240\u89c4\u5b9a\u683c\u5f0f\u7684\u6570\u636e,\u7136\u540e\u5b58\u5165\u4ee5\u8f93\u5165\u53c2\u6570\u7684\u503c\u4e3a\u5730\u5740\u7684\u53d8\u91cf\u4e2d\u3002
\u7528scanf()\u51fd\u6570\u4ee5%s\u683c\u5f0f\u8bfb\u5165\u7684\u6570\u636e\u4e0d\u80fd\u542b\u6709\u7a7a\u767d\u7b26\u65f6,\u6240\u6709\u7a7a\u767d\u7b26\u90fd\u88ab\u5f53\u505a\u6570\u636e\u7ed3\u675f\u7684\u6807\u5fd7\u3002\u6240\u4ee5\u9898\u4e2d\u51fd\u6570\u8f93\u51fa\u7684\u503c\u53ea\u6709\u7a7a\u683c\u524d\u9762\u7684\u90e8\u5206\u3002
\u5982\u679c\u60f3\u8981\u8f93\u51fa\u5305\u62ec\u7a7a\u683c\u5728\u5185\u7684\u6240\u6709\u6570\u636e,\u53ef\u4ee5\u4f7f\u7528gets()\u51fd\u6570\u8bfb\u5165\u6570\u636e\u3002gets()\u51fd\u6570\u7684\u529f\u80fd\u662f\u8bfb\u53d6\u5b57\u7b26\u4e32,\u5e76\u5b58\u653e\u5728\u6307\u5b9a\u7684\u5b57\u7b26\u6570\u7ec4\u4e2d,\u9047\u5230\u6362\u884c\u7b26\u6216\u6587\u4ef6\u7ed3\u675f\u6807\u5fd7\u65f6\u7ed3\u675f\u8bfb\u5165\u3002\u6362\u884c\u7b26\u4e0d\u4f5c\u4e3a\u8bfb\u53d6\u4e32\u7684\u5185\u5bb9,\u8bfb\u53d6\u7684\u6362\u884c\u7b26\u88ab\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7ed3\u675f\u6807\u5fd7'\0'\u3002
1、我们在main函数之前定义一个函数。
2、然后我们就可以开始编写,并且先输入整个框架。
3、然后我们可以用和循环开始进行输出。
4、利用普通变量也可以使用指针变量来书写。
5、在这里我们将以指针变量来书写。
6、书写完毕后,我们将可回去main函数中进行直接调用。
你可以写一个探测字符长度的函数:
# include <stdio.h>
//获取字符长度的函数:
int GetCharLong(char * C_P) {
int aa = 0;
while (1) {
if (*C_P == '\0') {
return aa;
}
C_P++;
aa++;
}
}
int main{
char a []="abc123";
char b []="123defw";
int aa = GetCharLong(a);
int bb = GetCharLong(b);
if (aa > bb){
printf("第一串长!%s\n", a);
}
else if (aa < bb){
printf("第二串长!%s\n", b);
}
else{
printf("两串字符一样长!");
}
return 0;
}
int strcmp(char *s,char *t)
{
for(;*s&&*t&&*s++==*t++;);
return(*s-*t);
}
#include<stdio.h>
#include<malloc.h>
#include<string.h>
int main()
{char *p[20];
int n,i,max=0,maxi=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i]=(char *)malloc(20);
scanf("%s",p[i]);
if(strlen(p[i])>max)
{
max=strlen(p[i]);
maxi=i;
}
}
printf("
%s
",p[maxi]);
return 0;
}
#include<stdio.h>
#include <string.h>
int main (void)
{
int n;
char str[1000];
char Max[1000];
scanf ("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%s", &str);
if(i==0) strcpy(Max, str);
if (strlen(Max)<strlen(str))
{
strcpy(Max, str);
}
}
printf ("The longest is: %s", Max);
return 0;
}
//by 紫阳
绛旓細1銆佹垜浠湪main鍑芥暟涔嬪墠瀹氫箟涓涓嚱鏁般2銆佺劧鍚庢垜浠氨鍙互寮濮缂栧啓锛屽苟涓斿厛杈撳叆鏁翠釜妗嗘灦銆3銆佺劧鍚庢垜浠彲浠ョ敤鍜屽惊鐜紑濮嬭繘琛岃緭鍑恒4銆佸埄鐢ㄦ櫘閫氬彉閲忎篃鍙互浣跨敤鎸囬拡鍙橀噺鏉ヤ功鍐欍5銆佸湪杩欓噷鎴戜滑灏嗕互鎸囬拡鍙橀噺鏉ヤ功鍐欍6銆佷功鍐欏畬姣曞悗锛屾垜浠皢鍙洖鍘籱ain鍑芥暟涓繘琛岀洿鎺ヨ皟鐢ㄣ
绛旓細鎬濊矾锛氬畾涔変竴涓彉閲弇ax锛屾帴鐫閿洏鍏杈撳叆涓涓鏁拌祴鍊肩粰max锛屾帴鐫while(n&&scanf("%d",&n)&&n)锛岀洿鍒拌緭鍏ラ浂涓烘锛屼緷娆″垽鏂緭鍏ョ殑鏁板拰max鐨勫ぇ灏忥紝纭繚max濮嬬粓鏄渶澶у硷紝鏈鍚庤緭鍑簃ax銆俰nclude<stdio.h>int main(){int n,max;scanf("%d",&n);max=n;while(n&&scanf("%d",&n)&&n){if(n>max...
绛旓細include<stdio.h>int main(){ int i,b; printf("杈撳叆瀛︾敓鏁伴噺锛"); scanf("%d",&b); float a[b],sum,max; for(i=0;i<b;i++) { printf("绗%d涓鐢熺殑鎴愮哗锛",i+1); scanf("%f",&a[i]); } max=a[0]; for(i=0;imax) max=a[i]; sum...
绛旓細include <stdio.h> include <stdlib.h> int main(){int max,min,num;printf("Input numbers,-1 to end:\n");scanf("%d",&num);max=min=num;while(num!=-1){ if(num>max)max=num;if(num<min)min=num;scanf("%d",&num);} printf("max:%d,min:%d\n",max,min);system("pause...
绛旓細濡傛灉鏄杈撳叆涓夎涓夊垪鐨勮瘽锛岃繖鏍峰氨鍙互浜嗭細婧愪唬鐮佸涓嬶細include<stdio.h>void main(){ int A[3][3],n,m,Logo,i,j,k;int pd(int a,int b,int c);for(n=0;n<3;n++)for(m=0;m<3;m++)scanf("%d",&A[n][m]);for(n=0;n<3;n++){Logo=pd(A[n][0],A[n][1],A[...
绛旓細include <stdio.h> fun(char *str){ int total,i=1;printf("杈撳叆瀛楃涓诧紝浠缁撴潫锛");str[1]=getche();while(str[i]!='@'){ i++; str[i]=getche(); } str[0]=i;printf("\n");for(i=1;i<str[0];i++)putch(str[i]);} main(){ char str[50];fun(str);s...
绛旓細{ int n=0;scanf("%d",s);while(*s!=0){ n++;s++;scanf("%d",s);} return n;} void statistics(int n, int s[], int *max, int *min, float *avg){ max=*min=*s;avg=0.0;for(;*s!=0;s++){ if(*s>*max) *max=*s;else if(*s<*min) *min=*s;avg+=*s;}...
绛旓細include"stdio.h"void f(int a[]){int i,x; while(1) {scanf("%d",&x); if(x==-1)break; a[x]++; }}int main(){int i,a[6]={0}; f(a); for(i=1;i<6;i++) printf("%d: %d\n",i,a[i]); return 0;} ...
绛旓細printf("鏁板瓧鐨勬讳釜浜嬫晠:%d\n",num[0]);printf("1鐨勪釜鏁%d\n",num[1]);printf("2鐨勪釜鏁%d\n",num[2]);printf("3鐨勪釜鏁%d\n",num[3]);printf("4鐨勪釜鏁ª%d\n",num[4]);printf("5鐨勪釜鏁%d\n",num[5]);printf("6鐨勪釜鏁%d\n",num[6]);printf("7鐨勪釜鏁%d\n",...
绛旓細include<stdio.h> void main锛堬級{ char str[];int i,num=0;printf("please input:\n");scanf("%s,str[]);for(i=0;str[i]!="@";i++)num++;printf("num:%d\n",num);}