码迷,mamicode.com
首页 > 编程语言 > 详细

C语言打印最长字符串

时间:2015-11-28 23:05:14      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

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

 char p[1000];
    printf("请输入字符串:");
 
   fgets(p, 1000, stdin);
    int i = 0;
  
    int high = 0;
    int low = 0;
    int low_temp = 0;
    int high_temp = 0;
    int count = 0;
    int temp = 0;
    for (i = 0; i < strlen(p); i++) {
        temp = 0;
        low_temp = i;
        while (p[i] != ‘ ‘ && p[i] != ‘\0‘) {
           temp++;
            i++;
        }
        high_temp = i-1;
        if (temp > count ) {
            count = temp;
            low = low_temp;
            high = high_temp;
        }
       
    }
    for (int i = low; i <= high; i++) {
        printf("%c",p[i]);
    }
    printf("\n");

C语言打印最长字符串

标签:

原文地址:http://www.cnblogs.com/yangsea/p/5003639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!