C Programming Tutorial

 
 
 
 
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

int d, m ;
long int y ;

void main( )
{
    char dates[5][12], temp[12] ;
    long int arr[5], t ;
    int i, j, valid ;

    int isvalid( ) ;
    long int getnum ( char * ) ;

    clrscr( ) ;

    printf ( \"Enter any five dates (dd\\\\mm\\\\yyyy) :-\\n\" ) ;
    for ( i = 0 ; i <= 4 ; i++ )
    {
        printf(\"%d) \",i + 1 ) ;
        scanf ( \"%s\", dates[i] ) ;

        /* Converts date in string to equivalent integer values. */
        arr[i] = getnum ( dates[i] ) ;

        /* Checks for valid date. */
        valid = isvalid( ) ;

        if ( !valid )
        {
            printf ( \"This date does not exists. \" ) ;
            printf ( \"Enter any other date.\\n\" ) ;
            i-- ;
        }
    }

    /* Sorting of dates using Bubble sort method. */
    for ( i = 0 ; i <= 3 ; i++ )
    {
        for ( j = 0 ; j <= 3 - i ; j++ )
        {
            if ( arr[j] > arr[j + 1] )
            {
                t = arr[j] ;
                arr[j] = arr[j + 1] ;
                arr[j + 1] = t ;

                strcpy ( temp, dates[j] ) ;
                strcpy ( dates[j], dates[j + 1]) ;
                strcpy ( dates[j + 1], temp ) ;
            }
        }
    }

    printf ( \"Dates in sorted order are :- \\n\" ) ;
    for ( i = 0 ; i <= 4 ; i++ )
        printf ( \"%s\\n\", dates[i] ) ;

    getch( ) ;
}

/* checks valid date */
int isvalid ( )
{
    int isleap ( long int ) ;

    if ( y <= 0 || m <= 0 || d <= 0 || m > 12 || y > 16384 ||
         ( m == 2 && !isleap ( y ) && d > 28 ) ||
         ( m == 2 && isleap ( y ) && d > 29 ) ||
         ( ( m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10
          || m == 12 ) && d > 31 ) || ( ( m == 4 || m == 6 || m == 9 ||
             m == 11 ) && d > 30 ) )
        return 0 ;
    else
        return 1 ;
}

/* checks whether the given year is leap or not */
int isleap ( long int y )
{
    return ( y % 4 == 0 ) ^ ( y % 100 == 0 ) ^ ( y % 400 == 0 ) ;
}

/* converts dates in string into equivalent integer values */
long int getnum ( char *date )
{
    char *p, str[15] ;
    long int num = 0 ;

    strcpy ( str, date ) ;

    d = m = y = 0 ;
    p = strtok ( str, \"\\\\\" ) ;
    if ( p != NULL )
        d = num = atoi ( p )  ;

    p = strtok(NULL, \"\\\\\");
    if ( p != NULL )
        m = atoi ( p ) ;
    num += m * 32 ;

    p = strtok(NULL, \"\\\\\");
    if ( p != NULL )
        y = atol ( p ) ;
    num += y * 512L ;

    return num ;
}

    Related Post:
  1. Program that takes a string from user and prints the total number of characters, words and lines present in the string.

  2. Function for inserting an item into a linked list

  3. Find Death Date C Program

  4. Program to compare one string s2 to another string s1. Print the difference of first unmatched characters using strcmp function from string.h

  5. PROGRAM TO COMPUTE n! USING SELF-SCHEDULING

  6. Program to copy one string s2 to another string s1. (Use strcopy function from string.h)

  7. GAUSS SEIDEL METHOD

  8. PROGRAM OF CREATE HEADER FILE FOR SHARED MEMORY

  9. Program to display menus like windows operating system and also can add as many as user want

  10. Program to find area of rectangular

  11. Program to display entered character in capital letter

  12. Program to interchange the Small and Capital Letters and Unload the TSR on occurance of @ character

  13. Program to convert an expression in postfix form to an infix form

  14. Program of 2-thread for Linux Using the POSIX threads library. Once the thread spawning is done, the first thread should wait for user input

  15. Implement a multi-access threaded queue with multiple threads inserting and multiple threads extracting from the queue

  16. PROGRAM TO APPLY BLOCK SCHEDULING TECHNIQUE TO SOLVE FOLLOWING EXPRESSION X[I] = X[I+1] + Y[I]

  17. Linear search in an unsorted array

  18. Program to find sum, average and deviation of input numbers using parallel processing

  19. Program that displays use of extern variable

  20. Program to multiply two nXn matrix using indirect scheduling in two dimensional matrix

 
 
Didn't find what you were looking for? Find more on Sorting of dates using bubble sort