Task: Ordered words

C: #include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>


#define MAXLEN 100
typedef char TWord[MAXLEN];


typedef struct Node {
    TWord word;
    struct Node *next;
} Node;


int is_ordered_word(const TWord word) {
    assert(word != NULL);
    int i;

    for (i = 0; word[i] != '\0'; i++)
        if (word[i] > word[i + 1] && word[i + 1] != '\0')
            return 0;

    return 1;
}


Node* list_prepend(Node* words_list, const TWord new_word) {
    assert(new_word != NULL);
    Node *new_node = malloc(sizeof(Node));
    if (new_node == NULL)
        exit(EXIT_FAILURE);

    strcpy(new_node->word, new_word);
    new_node->next = words_list;
    return new_node;
}


Node* list_destroy(Node *words_list) {
    while (words_list != NULL) {
        Node *temp = words_list;
        words_list = words_list->next;
        free(temp);
    }

    return words_list;
}


void list_print(Node *words_list) {
    while (words_list != NULL) {
        printf("\n%s", words_list->word);
        words_list = words_list->next;
    }
}


int main() {
    FILE *fp = fopen("unixdict.txt", "r");
    if (fp == NULL)
        return EXIT_FAILURE;

    Node *words = NULL;
    TWord line;
    unsigned int max_len = 0;

    while (fscanf(fp, "%99s\n", line) != EOF) {
        if (strlen(line) > max_len && is_ordered_word(line)) {
            max_len = strlen(line);
            words = list_destroy(words);
            words = list_prepend(words, line);
        } else if (strlen(line) == max_len && is_ordered_word(line)) {
            words = list_prepend(words, line);
        }
    }

    fclose(fp);
    list_print(words);

    return EXIT_SUCCESS;
}










Fortran: 
 module ordered_module

 implicit none 

	
	integer,parameter :: file_unit = 1000
	character(len=*),parameter :: filename = 'unixdict.txt'

	
	integer,parameter :: max_chars = 50

	type word 
	  character(len=max_chars) :: str    
	  integer	:: n = 0    
	  logical	:: ordered = .false.    
	end type word
 
	
	type(word),dimension(:),allocatable :: dict
 
	contains

 
	
		function count_lines_in_file(fid) result(n_lines)
	
		implicit none
 
		integer             :: n_lines
		integer,intent(in)  :: fid		
		character(len=1)    :: tmp
		integer             :: i
		integer             :: ios
 
		
 
		rewind(fid)	  
 
		n_lines = 0
		do 
			read(fid,'(A1)',iostat=ios) tmp
			if (ios < 0) exit      
			n_lines = n_lines + 1  
		end do
 
		rewind(fid)   
 
	
		end function count_lines_in_file
	

	
	 pure elemental function ordered_word(word) result(yn)
	
	
	
	 
	 implicit none
	 character(len=*),intent(in) :: word
	 logical :: yn
	 
	 integer :: i
	 
	 yn = .true.
	 do i=1,len_trim(word)-1
	 	if (ichar(word(i+1:i+1))<ichar(word(i:i))) then
	 		yn = .false.
	 		exit
	 	end if
	 end do
	 
	
	 end function ordered_word
	
 

 end module ordered_module



 program main

 use ordered_module
 implicit none

	integer :: i,n,n_max
 
	
	open(unit=file_unit,file=filename)     		
	n = count_lines_in_file(file_unit)      
	allocate(dict(n))                       
	do i=1,n                                
		read(file_unit,'(A)') dict(i)%str   
		dict(i)%n = len_trim(dict(i)%str)   
	end do		
	close(file_unit)                        

	
	dict%ordered = ordered_word(dict%str)	

	
	n_max = maxval(dict%n, mask=dict%ordered)
		
	
	do i=1,n
		if (dict(i)%ordered .and. dict(i)%n==n_max) write(*,'(A,A)',advance='NO') trim(dict(i)%str),' '
	end do
	write(*,*) ''


 end program main