求出句子中最长的单词

问题遇到的现象和发生背景

一个以‘.’结尾的简单英文句子,单词之间用空格分隔,没有缩写形式和其它特殊形式。

【输入】
一个以‘.’结尾的简单英文句子(长度不超过500),单词之间用空格分隔,没有缩写形式和其它特殊形式。

【输出】
该句子中最长的单词。如果多于一个,则输出第一个。

问题相关代码,请勿粘贴截图
#include<bits/stdc++.h>
using namespace std;
int main(){
    int l,max=0;
    string a[256];
    for(int i=0;i<256;i++){
        scanf("%s",a[i]);
        l=strlen(a[i]);
        if(l>max){
            max=l;
        }
    printf("%s",a[max]);
    return 0;
}


运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
#include <stdio.h>
#include <ctype.h>

#define N 500

int main()
{
    char a[N];
    char ch;
    int i = 0;
    while ((ch = getchar()) != EOF)
    {
        if (ch == '.')
            break;
        a[i++] = ch;
    }
    const char *p = a;
    int max_count = 0;
    const char *first = NULL, *last = NULL;
    while (*p)
    {
        if (isspace(*p))
        {
            do
            {
                p++;
            } while (*p && isspace(*p));
        }
        else
        {
            const char *q = p;
            do
            {
                p++;
            } while (*p && !isspace(*p));
            int count = p - q;
            if (count > max_count)
            {
                max_count = count;
                first = q;
                last = p;
            }
        }
    }
    for (; first != last; ++first)
        putc(*first, stdout);
    return 0;
}