Write a program that finds the first longest and smallest word in a string.


Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <string.h>
#define SIZE 100
void main()
{
	char str[SIZE], maxstr[SIZE]="", minstr[SIZE]="";	

        printf("Enter the string ended with a dot:\n");
	scanf("%s", str);
 
	strcpy(maxstr, str);
	strcpy(minstr, str);
	while(strcmp(str,".")!=0)
	{
		if(strlen(str)>strlen(maxstr))
			strcpy(maxstr, str);
		if(strlen(str)<strlen(minstr))
			strcpy(minstr, str);
		scanf("%s", str);
	}
	

       printf("Longest word=\"%s\"\n",maxstr);
       printf("Smallest word=\"%s\"\n",minstr);
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Kruskal Algorithm - Minimal Spanning Tree