(函数)判断一句话中最长的单词(C语言)

一、运行结果;

二、源代码;

# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>

//声明函数;
int aiphabetic(char);
int longest(char[]);

int main()
{
	//初始化变量值;
	int i;
	char line[100] = { 0 };

	//获取用户输入字符串;
	printf("请输入一句话:");
	gets(line);

	//输出最长单词;
	printf("最长单词为:");
	for (i = longest(line); alphabetic(line[i]); i++)
	{
		//输出;
		printf("%c", line[i]);
	}

	//换行;
	printf("\n");

	return 0;
}

//实现函数;
int alphabetic(char c)
{
	//判断;
	if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int longest(char string[])
{
	//初始化变量值;
	int len = 0, i, length = 0, flag = 1, place = 0, point;

	//循环判断;
	for (i = 0; i <= strlen(string); i++)
		if (alphabetic(string[i]))
			if (flag)
			{
				point = i;
				flag = 0;
			}
			else
			{
				len++;
			}
		else
		{
			flag = 1;
			if (len >= length)
			{
				length = len;
				place = point;
				len = 0;
			}
		}

	return place;
}

上一篇:(持续更新)ai 工程师(nlp)面试问题+答案


下一篇:Next前端是什么