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;
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. ABC-WORDS.
       
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT DICT ASSIGN TO DISK
           ORGANIZATION LINE SEQUENTIAL.
           
       DATA DIVISION.
       FILE SECTION.
       FD DICT 
           LABEL RECORD STANDARD
           VALUE OF FILE-ID IS "unixdict.txt".
       01 ENTRY.
          03 WORD         PIC X(32).
          03 LETTERS      PIC X OCCURS 32 TIMES, REDEFINES WORD.
       
       WORKING-STORAGE SECTION.
       01 LEN             PIC 99.
       01 MAXLEN          PIC 99 VALUE 0.
       01 I               PIC 99.
       01 OK-FLAG         PIC X.
          88 OK           VALUE '*'.
       
       PROCEDURE DIVISION.
       BEGIN.
           OPEN INPUT DICT.
           
       FIND-LONGEST-WORD.
           READ DICT, AT END CLOSE DICT, GO TO PRINT-LONGEST-WORDS.
           PERFORM CHECK-WORD.
           GO TO FIND-LONGEST-WORD.
           
       PRINT-LONGEST-WORDS.
           ALTER VALID-WORD TO PROCEED TO SHOW-WORD.
           OPEN INPUT DICT.
       
       READ-WORDS.
           READ DICT, AT END CLOSE DICT, STOP RUN.
           PERFORM CHECK-WORD.
           GO TO READ-WORDS.
           
       CHECK-WORD.
           MOVE ZERO TO LEN.
           INSPECT WORD TALLYING LEN 
               FOR CHARACTERS BEFORE INITIAL SPACE.
           MOVE '*' TO OK-FLAG.
           PERFORM CHECK-CHAR-PAIR VARYING I FROM 2 BY 1
               UNTIL NOT OK OR I IS GREATER THAN LEN.
           IF OK, PERFORM DO-WORD.
       
       CHECK-CHAR-PAIR.
           IF LETTERS(I - 1) IS GREATER THAN LETTERS(I), 
               MOVE SPACE TO OK-FLAG.
         
       DO-WORD SECTION.         
       VALID-WORD.
           GO TO CHECK-LENGTH.
       CHECK-LENGTH.
           IF LEN IS GREATER THAN MAXLEN, MOVE LEN TO MAXLEN.
           GO TO DONE.
       SHOW-WORD.
           IF LEN IS EQUAL TO MAXLEN, DISPLAY WORD.
       DONE.
           EXIT.