编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词。

时间:2014-08-15 10:53:29   收藏:0   阅读:278

 char str[] = "my beautiful teacher is  ";

    unsigned long  int a = 0;   //长整型变量

    a = strlen(str);

    int count = 0, max = 0 ;

    for (int i = 0; i < a; i ++) {

            if (str[i] != ‘ ‘) {

                count ++;

                continue;

            }

            max =count > max ? count : max;

            count = 0;

        }

    printf("%d\n",max);   //求最大值

    printf("最长单词为:");

    for (int i = 0; i < a; i ++) {

        if (str[i] != ‘ ‘) {

            count ++;

            if (count == max) {

                for (int j = i - max  ; j <= i ; j ++) {

                    printf("%c", str[j]);

                }

            }

            continue;

        }

        count = 0;

    }


编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词。,布布扣,bubuko.com

原文:http://chenhongyi.blog.51cto.com/9168548/1540278

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!