A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© buzheng 初级黑马   /  2016-5-17 11:49  /  1130 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

查找语句中的最长单词

2 个回复

倒序浏览
#include<stdio.h>
#include<string.h>
#define M 1000
int main()
{
    int low = 0;        // 单词的起始下标
    int high = 0;        // 单词的结束位置
    int i;            // 循环变量
    int count = 0;        // 统计最长单词的长度
    int temp;        // 中间变量
    int low_temp;
    int high_temp;
    char p[M];        // 存储有多个单词的字符指针
    gets(p);
    for(i = 0; i < strlen(p); i++)
    {
        temp = 0;
        low_temp = i;
        while(p[i] != ' ' && p[i] != '\0')    // p[i] != 空格
        {
            temp++;
            i++;
        }
        high_temp = i-1;
        if(temp > count)
        {
            count = temp;
            low = low_temp;
            high = high_temp;
        }
    }
    for(i = low; i <= high; i++)
    {
        putchar(p[i]);
    }
    printf("\n");
    return 0;
}
回复 使用道具 举报
楼上正解
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马