Apr 6, 2012

C Program to delete a file.

Write a C Program to delete a file.
To delete a file in c, We use the remove macro, which is defined in stdio.h. remove macro takes filename with extension as its argument and deletes the file, if it is in the directory and returns the zero, if deleted successfully.
Note that deleted file does not goes to the recycle bin, it is going to delete permanently.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#include<process.h>
main()
{
    FILE *fp;
    char filename[20];
    int status;
    clrscr();
    printf("\nEnter the file name:\n\n");
    scanf("%s",filename);
    
    fp = fopen(filename, r);
    
    if(fp == NULL)
    {
        printf("Error:  file not found! check the directory!!\n\n");
        exit(0);
    }
    fclose(fp);
    status = remove(file_name);
    
    if( status == 0 )
    printf("%s file deleted successfully.\n",file_name);
    else
    {
        printf("Unable to delete the file\n");
        perror("Error");
    }
    
    return 0;
}
Read more Similar C Programs
C Basic

C File i/o
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



To browse more C Programs visit this link

Apr 4, 2012

C Program to demonstrate strspn function.

Write a C Program to demonstrate strspn function.
The strspn() function returns the index of the first character in string1 that doesn't match any character in string2. If all the characters of the string2 matched with the string1, strspn returns the length of the string1. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
    int spn;
    char str1[] = "1234hjbcde09i";
    char str2[] = "12345";
    char str3[] = "a12d3456fd";
    char char_list[] = "1234567890";
    clrscr();
    //It returns 4
    spn = strspn (str1,char_list);
    printf ("\n\nThe length of the initial numbers for str1 is %d.\n",spn);
    
    //It returns 5
    spn = strspn (str2,char_list);
    printf ("\nThe length of the initial numbers for str2 is %d.\n",spn);
    
    //It returns 0
    spn = strspn (string3,char_list);
    printf ("\nThe length of the initial numbers for string3 is %d.\n",spn);
    return 0;
}

Read more c programs
C Basic
C Strings
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link

Apr 3, 2012

C program to demonstrate ceil function.

Write a C program to demonstrate ceil function. ceil is in the C math.h library.
ceil function rounds up the smallest next integral value.
Example:ceil(2.0) is 2.0
ceil(2.1) is 3.0
ceil(2.2) is 3.0
--
--
ceil(2.9) is 3.0
Read more about C Programming Language.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/




#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
    double number, result;
    clrscr();
    printf("Enter a number to round it up\n");
    scanf("%lf",&number);
    
    result = ceil(number);
    
    printf("\nOriginal number =  %lf\n", number);
    printf("\nNumber rounded up = %lf\n", result);
    getch();
    return 0;
} 
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

C program to draw a circle using c graphics.

Write C program to draw a circle using c graphics.
In this program we use the graphics.h library function to draw a circle. To use graphics.h, we have to install the drivers in to the the system by using the initgraph() function. In the program circle is the graphic function , which takes three parameters, first two are co-ordinates and the third one is the radius of the circle. Read more about C Programming Language.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <graphics.h>

main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TC\\BGI");
    
    circle(25, 25, 100);
    
    getch();
    closegraph();
    return 0;
}
Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

Apr 2, 2012

C Program to compute the difference between two dates.

Write a C program to compute the difference between two dates. In this C Program, We check the date is valid or not and then calculating the No. of days of first date and second date from Jan 1 of first date, then subtract the smaller date from another. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
    int day1,mon1,year1,day2,mon2,year2;
    int ref,dd1,dd2,i;
    clrscr();
    printf("Enter first date  day, month, year\n");
    scanf("%d%d%d",&day1,&mon1,&year1);
    printf("Enter second date day, month, year\n");
    scanf("%d%d%d",&day2,&mon2,&year2);
    ref = year1;
    if(year2<year1)
    ref = year2;
    dd1=0;
    dd1=dater(mon1);
    for(i=ref;i<year1;i++)
    {
        if(i%4==0)
        dd1+=1;
    }
    dd1=dd1+day1+(year1-ref)*365;
    dd2=0;
    for(i=ref;i<year2;i++)
    {
        if(i%4==0)
        dd2+=1;
    }
    dd2=dater(mon2)+dd2+day2+((year2-ref)*365);
    printf("\n\n Difference between the two dates is %d days",abs(dd2-dd1));
    
    getch();
}




int dater(x)
{ int y=0;
    switch(x)
    {
        case 1: y=0; break;
        case 2: y=31; break;
        case 3: y=59; break;
        case 4: y=90; break;
        case 5: y=120;break;
        case 6: y=151; break;
        case 7: y=181; break;
        case 8: y=212; break;
        case 9: y=243; break;
        case 10:y=273; break;
        case 11:y=304; break;
        case 12:y=334; break;
        default: printf("Invalid Input\n\n\n\n"); exit(1);
    }
    return(y);
}

Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

C program to replace the sub string with another string.

C Strings:
Write a C program to replace the sub string in a string with another string.
In this program, We replace string with another string, if the entered string is the sub string of main string, otherwise function replace_str returns the original string as it is. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

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

char *replace_str(char *str, char *orig, char *rep)
{
    static char buffer[4096];
    char *p;
    
    if(!(p = strstr(str, orig)))
    return str;
    
    strncpy(buffer, str, p-str);
    buffer[p-str] = '\0';
    
    sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
    
    return buffer;
}

int main(void)
{
    char str[100],str1[50],str2[50];
    printf("Enter a one line string..\n");
    gets(str);
    printf("Enter the sub string to be replaced..\n");
    gets(str1);
    printf("Enter the replacing string....\n");
    gets(str2);
    puts(replace_str(str, str1, str2));
    
    return 0;
}
Read more Similar C Programs
Searching in C

C Strings
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

Apr 1, 2012

C Program to find all the permutations of a given string.

Write a c program to find all the permutations of a given string.
Example: If the given string is sam, output is,
sam
sma
msa
mas
asm
ams.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

# include<stdio.h>
# include<conio.h>
#include<string.h>
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void permute(char *a, int i, int n)
{
    int j;
    if (i == n)
    printf("%s\n", a);
    else
    {
        for (j = i; j <= n; j++)
        {
            swap((a+i), (a+j));
            permute(a, i+1, n);
            swap((a+i), (a+j));
        }
    }
}


int main()
{
    char str[80] ;
    int len=0,i;
    puts("Enter a string:\n\n");
    gets(str);
    for (i=0; str[i] != '\0'; i++)
    {
        len++;
    }
    permute(str, 0, len);
    getchar();
    return 0;
}
Read more Similar C Programs
Searching in C

C Strings
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link