正文

[054] 找出最长及最短字符串2006-04-23 17:51:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/wentao/13085.html

分享到:

<C程序设计> (夏宝岚)练习字符串处理: 编写程序,由键盘输入若干字符串,输出最长及最短的字符串。#include <stdio.h>#include <string.h>int main(){    char  InPut[1000];    char OutMax[1000];    char OutMin[1000];    int len;    int max = 0;    int min = 1000;    printf("InPut a string: ");    gets(InPut);    while ((len = strlen(InPut)) > 0)    {            if (len > max)        {            max = len;            strcpy(OutMax, InPut);        }        if (len < min)        {            min = len;            strcpy(OutMin, InPut);        }        printf("InPut a string: ");        gets(InPut);        if(strcmp(InPut, "\0") == 0)              /* 注释1 */            break;    }    if (max > 0)        printf("\nLongest string:  %s\n", OutMax);    if (len != 0)                                 /* 注释2 */        printf("Shortest string: %s\n", OutMin);    return 0;}运行结果:===============================================InPut a string: adfkaldflakfp3ekfak↙InPut a string: ;ldkfl↙InPut a string: flsdkf↙InPut a string: d;lfae3↙InPut a string: ad↙InPut a string: ld↙InPut a string: d↙InPut a string: lfdkald;↙InPut a string:↙ Longest string:  adfkaldflakfp3ekfakShortest string: d===============================================注释1: 由于用的是while的结果,加此句是为了最短字符串的输出。如果不加此句在最后输入一个回车时,还要执行一个while ((len = strlen(InPut)) > 0)判断,此时len被赋值为0,输出最短字符串条件不成立(注释2)。加了此句后输入最后一个回车后便直接跳出循环,此时len为上一个字符串长度,不为零。注释2: 如果一个字符串也不输入,直接回车,则不会执行循环体,且此时len已被赋值为0,若不加此句,则会出错。★ 本程序利用while 循环反复从键盘输入字符串,条件((len = strlen(InPut)) > 0) 表示当不输入任何内容直接按回车键时,使len的值为0来结束循环。 用二维数组实现如下:#include <stdio.h>#include <string.h>#define N 100#define M 80int main(){    char str[N][M];    char temp[M];   /* 临时串   */    char Max[M];    /* 存最长串 */    char Min[M];    /* 存最短串 */    int i = 0;    int count;     printf("Input string (empty string to end)\n");        gets(temp);        while (strcmp(temp, "\0") != 0 && i < N)  /* 若temp为非空串,则继续循环*/                                              /* 不可写成'\0' */    {        strcpy(str[i], temp);        i++;        gets(temp);        if (i == N)            break;    }     count = i;    strcpy(Max, str[0]);               /* 将第一个串作为最长串 */    strcpy(Min, str[0]);               /* 将第一个串作为最短串 */     for (i = 1; i < count; i++)    {        if (strlen(Max) < strlen(str[i]))   /* 最大串放入Max */            strcpy(Max, str[i]);        if (strlen(Min) > strlen(str[i]))   /* 最小串放入Min */            strcpy(Min, str[i]);    }    printf("Max: %s\n", Max);    printf("Min: %s\n", Min);    return 0;} 运行结果:=========================================Input string (empty string to end)a;dkf;dk↙;sdk↙;sdkf;dflk;fk↙sd;lf↙d;lf;dfk;dkf;akdf;kf↙sd↙d;flk;dfk;↙↙Max: d;lf;dfk;dkf;akdf;kfMin: sd=========================================★ 其实此例仅可用来练习一下字符数组的一些操作,没什么实际意义,因为对一些细节根本没做处理,比如对相同长度字符串就没有处理, 若最长或最短的字符串有不止一个,则只会输出最后处理得到的结果,而其他的并未输出。    换句话说上述程序根本未完成题目的要求,应该将所有最长的字符串及所有的字符串全部输出。如何实现呢?思考中……  想来也简单,只要在输出时将最后得到的最长最短串和所有串比一下,一样长的就输出就可以了:#include <stdio.h>#include <string.h>#define N 100#define M 80int main(){    char str[N][M];    char temp[M];   /* 临时串   */    char Max[M];    /* 存最长串 */    char Min[M];    /* 存最短串 */    int i = 0;    int count;     printf("Input string (empty string to end)\n");        gets(temp);        while (strcmp(temp, "\0") != 0 && i < N)  /* 若temp为非空串,则继续循环*/                                              /* 不要写成'\0' */    {        strcpy(str[i], temp);        i++;        gets(temp);        if (i == N)            break;    }     count = i;    strcpy(Max, str[0]);               /* 将第一个串作为最长串 */    strcpy(Min, str[0]);               /* 将第一个串作为最短串 */     for (i = 1; i < count; i++)    {        if (strlen(Max) < strlen(str[i]))   /* 最大串放入Max */            strcpy(Max, str[i]);        if (strlen(Min) > strlen(str[i]))   /* 最小串放入Min */            strcpy(Min, str[i]);    }        printf("Max:\n");    for (i = 0; i < count; i++)        if (strlen(Max) == strlen(str[i]))            printf("%s\n", str[i]);     printf("\nMin: \n");    for (i = 0; i < count; i++)        if (strlen(Min) == strlen(str[i]))            printf("%s\n", str[i]);     return 0;}运行结果:================================================Input string (empty string to end)abcd↙abcde↙abcdefg↙aaa↙asldfef↙bbb↙ccccccc↙ldfj↙sss↙↙Max:abcdefgasldfefccccccc Min:aaabbbsss================================================★ 还是感觉按上述思路做有些繁琐,应该可以做的简单些,继续思考……     还是用上述思路,只是将中间的用最长的字符串与后续的字符串比较换成了用最长字符串的长度与后续字符串长度比较, 看上去要简洁些。定义了一个最大值Max和一个最小值Min,循环取字符串过程中将最长字符串的长度赋于Max,最短长度赋于Min。再通过一次循环将所有长度等于Max的字符串(即最长串)和长度等于Min的字符串(即最短串)分别输出。#include <stdio.h>#include <string.h>#define N 100#define M 80int main(){    char str[N][M];    char temp[M];   /* 临时串           */    int Max = 0;    /* 记录最长串的长度 */    int Min = M;    /* 记录最短串的长度 */    int i = 0;    int count;     printf("Input string (empty string to end)\n");        gets(temp);        while (strcmp(temp, "\0") != 0 && i < N)  /* 若temp为非空串,则继续循环*/                                              /* 不要写成'\0' */    {        strcpy(str[i], temp);        i++;        gets(temp);        if (i == N)            break;    }     count = i;     for (i = 0; i < count; i++)    {        if (Max < strlen(str[i]))            Max = strlen(str[i]);        if (Min > strlen(str[i]))            Min = strlen(str[i]);    }        printf("Max:\n");    printf("-----------------------------------------\n");     for (i = 0; i < count; i++)        if (Max == strlen(str[i]))            printf("%s\n", str[i]);     printf("-----------------------------------------\n");    printf("\nMin:\n");    printf("-----------------------------------------\n");     for (i = 0; i < count; i++)        if (Min == strlen(str[i]))            printf("%s\n", str[i]);     printf("-----------------------------------------\n");     return 0;} 运行结果:===========================================================Input string (empty string to end)asdfkdfllklf↙dlkffldk↙lasjlfladflflajdfl↙dlkfjsdlfoefkd↙ldkfdlfk↙lskd↙ldjfldjfadlsfeofds↙ldfk↙ldfjldjfldfldfdfdd↙dkkkk↙↙Max:-----------------------------------------lasjlfladflflajdflldjfldjfadlsfeofdsldfjldjfldfldfdfdd----------------------------------------- Min:-----------------------------------------lskdldfk-----------------------------------------===========================================================

阅读(4699) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册