Saturday, 15 March 2014

Code for Program to get string after user specified position in C Programming

Code for Program to get string after user specified position in C Programming


//xstrchr() fn.


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

void main(){
     char * xstrchr(char *,char);
     charstring[15];
     char *ptr,c;
    clrscr();
    printf("Enter a string : ");
    scanf("%[^\n]s",string);
    printf("Enter search character : ");
    flushall();
    scanf("%c",&c);
    ptr = xstrchr(string, c);
    printf("\nOutput of function is : %s\n",ptr);
    if (ptr)
       printf("The character %c is at position: %d\n", c, ptr-string);
    else
       printf("The character was not found\n");
    getch();
}

char * xstrchr(char *str,char ch){
       int i;
       for(i=0;str[i];i++){
       if(str[i]==ch)
          return ((str+i)+1);
       }
       return(0);
}

FIND VALUE OF X1 AND X2 WHICH IS SOLUTION OF LINEAR EQUATION

PROGRAM TO FIND VALUE OF X1 AND X2 WHICH IS SOLUTION OF LINEAR EQUATION

Code for PROGRAM TO FIND VALUE OF X1 AND X2 WHICH IS SOLUTION OF LINEAR EQUATION in C Programming

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

void main()
{
    int a,b,c,d,m,n;
    float x1,x2;
    clrscr();

    printf("Enter the value of a ::  ");
    scanf("%d",&a);

    printf("Enter the value of b ::  ");
    scanf("%d",&b);

    printf("Enter the value of c ::  ");
    scanf("%d",&c);

    printf("Enter the value of d ::  ");
    scanf("%d",&d);

    printf("Enter the value of m ::  ");
    scanf("%d",&m);

    printf("Enter the value of n ::  ");
    scanf("%d",&n);

    if((a*d)-(c*b) == 0)
    printf("\nWe can't find value of x1 and x2 \n");
    else
    {
        x1 = (float)((m * d) - (b * n)) / ((a * d) - (c * b));
        printf("\nValue of x1 ::  %f ",x1);
        x2 = (float)((n * a) - (m * c)) / ((a * d) - (c * b));
        printf("\nValue of x2 ::  %f ",x2);

    }
    getch();
}
/*
    ********
     OUTPUT
    ********
    Enter the value of a ::  5
    Enter the value of b ::  2
    Enter the value of c ::  5
    Enter the value of d ::  3
    Enter the value of m ::  6
    Enter the value of n ::  7

    Value of x1 ::  0.800000
    Value of x2 ::  1.000000
*/

ode for Program to search and/or replace a word in C Program in C Programming

Write a program to search and/or replace a word in C Program. It asks for an input file and 2 string. One for searching and one for replacement.If the file contains search string than it will print that line containing search string and asks for the confirmation to change it.If you say "yes" it changes it else continues till the file is over.

Code for Program to search and/or replace a word in C Program in C Programming

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

count_data();

void main()
{
   // calling function
   count_data();
   getch();
}
count_data() // function for count no of words,lines & characters.
{
   FILE *fp,*fp_rep;
   char ch,ch1,temp_str[50],rep_str[10],new_str[10];
   int count=0; // counter
   clrscr();


   fp=fopen("c:\\windows\\desktop\\input.txt","r");
   fp_rep=fopen("c:\\windows\\desktop\\input1.txt","w");

   printf("\nEnter String to find:");
   scanf("%s",rep_str);
   printf("\nEnter String to replace:");
   scanf("%s",new_str);

   while((ch=getc(fp))!=EOF)
   {
     if(ch==' ')
      {
    temp_str[count]='\0';
    if(strcmp(temp_str,rep_str)==0)
     {
      printf("Do u want to replace(y/n):");
      ch1=getche();
      if(ch1=='y')
      {
       fprintf(fp_rep," %s",new_str);
       count=0;
      }
      else
       { fprintf(fp_rep," %s",temp_str);count=0;}
     }
    else
     {
       fprintf(fp_rep," %s",temp_str);
       count=0;
      }
      }else
      {
    temp_str[count++]=ch;
      }
    }
      if(strcmp(temp_str,rep_str)==0)
     {

      printf("Do u want to replace(y/n):");
      ch1=getche();
      if(ch1=='y')
      {
       fprintf(fp_rep,"%s ",new_str);
       count=0;
      }
      else
       {
        fprintf(fp_rep,"%s ",temp_str);
        count=0;
       }
     }else
      {
       fprintf(fp_rep,"%s ",temp_str);
      }

    fclose(fp);
    fclose(fp_rep);
    remove("c:\\windows\\desktop\\input.txt");
    rename("c:\\windows\\desktop\\input1.txt","c:\\windows\\desktop\\input.txt");
    fflush(stdin);

}

Write a program to sort a list in alphabatic order using pointers.

Write a program to sort a list in alphabatic order using pointers.

Code for Program to sort a list in alphabatic order using pointers in C Programming

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <alloc.h>
void main()
{

char *a[10],dum[10],s;
int i,k,j,n;
clrscr();
printf("enter the no of std....");
scanf("%d",&n);
printf("enter the name of students ");
for(k=0;k<n;k++)
scanf("%s",a[k]);
for(i=1;i<n;i++)
{
for(j=1;j<n-i;j++)
{if(strcmp(a[j-1],a[j])>0)
  {strcpy(*dum,*a[j-1]);
   strcpy(*a[j-1],*a[j]);
   strcpy(*a[j],*dum);
 }
 }  }
 for(i=0;i<n;i++)
 printf("%s",a[i]);
 getch();
 }

Write a program of hanoi tower.

Write a program of hanoi tower.

Code for Program of hanoi tower in C Programming

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

void main()
{
  int n;
  int hanoi(int,char,char,char);
  clrscr();
  printf("Enter  the no of disk:");
  scanf("%d",&n);
  hanoi(n,'a','b','c');
  getch();
}
int hanoi(int n,char a, char b, char c)
{
while(n!=0)
{
hanoi(n-1,a,c,b);
printf("Moving disk %d from %c tower to %c tower\n",n,a,c);
hanoi(n-1,b,a,c);
break;
}
return 0;

}

Example of using preprocessor - 4 in different files.

Example of using preprocessor - 4 in different files.

Code for Example of using preprocessor - 4 in different files in C Programming

//file1.h#define USD 1

//file2.h#define UKP 1

//file3
#include <stdio.h>
#include <file1.h>               //A#if !defined (USD) || !defined (UKP)      // B#error “ERROR: NO_CURRENCY rate is specified.”  //C#endif

 main()
{        
    int rs;
    rs = 10 * currency_rate;          //D
    printf (“%d\n”, rs);
}

Write a program to calculate range of v

Write a program to calculate range of v
Code for Program to calculate range of values in C Programmingalues.

main()                                                      
   {                                                           
       int count;                                                   
       floatvalue, high, low, sum, average, range;                              
       sum = 0;                                                
       count = 0;                                              
       printf("Enter numbers in a line :                       
                 input a NEGATIVE number to end\n");           
   input:                                                      
       scanf("%f", &value);                                    
       if (value < 0) goto output;                             
          count = count + 1;                                   
       if (count == 1)                                         
          high = low = value;                                  
       elseif (value > high)                                  
               high = value;                                   
            elseif (value < low)                              
                 low = value;  
      sum = sum + value;                                       
      goto input;  

                                            
   output:                                                     
      average = sum/count;                                         
      range = high - low;                                      
      printf("\n\n");                                          
      printf("Total values : %d\n", count);                    
      printf("Highest-value: %f\nLowest-value : %f\n",         
               high, low);                                     
      printf("Range        : %f\nAverage      : %f\n",         
               range, average);                                
   }                                                           
                                                               
   
Output                                                      


                                                               
   Enter numbers in a line : input a NEGATIVE number to end    
   35  40.50  25  31.25  68.15  47  26.65  29  53.45  62.50 -1 
                                                               
   Total values : 10                                           
   Highest-value: 68.150002                                    
   Lowest-value : 25.000000                                    
   Range        : 43.150002                                    
   Average      : 41.849998 

Sunday, 9 March 2014

A car garage simulation using de-queue (link list implementation).

A car garage simulation using de-queue (link list implementation).

Code for A car garage simulation using de-queue (link list implementation) in C Programming

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

#define TOP 1
#define BOT 2

struct node
{
    char plate [15] ;
    struct node *link ;
} *front[5], *rear[5] ;

char plate[15], temp[15] ;
int i ;

void add_dq ( struct node**, struct node**, int, char* ) ;
char* del_dq ( struct node**, struct node**, int ) ;
void push ( struct node**, char* ) ;
char* pop ( struct node** ) ;
void q_display ( struct node * ) ;

void main( )
{
    char ad ;
    int s, lane = -1, min, lc ;

    clrscr( );
    while ( 1 )
    {
        for ( i = 0 ; i < 5 ; i++ )
        {
            printf( "lane %d: ", i ) ;
            q_display ( front[i] ) ;
        }

        printf( "\nArrival/Departure/Quit? ( A/D/Q ): " ) ;
        ad = getch( ) ;

        if ( toupper ( ad ) == 'Q' )
            exit ( 1 ) ;

        printf ( "\nEnter license plate num:" ) ;
        gets ( plate ) ;
        ad = toupper ( ad ) ;

        if ( ad == 'A' )  /* arrival of car */

        {
            lane = -1 ;  /* assume no lane is available */

            min = 10 ;
            for ( i = 0 ; i < 5 ; i++ )
            {
                s = count ( front[i] ) ;
                if ( s < min )
                {
                    min = s ;
                    lane = i ;
                }
            }

            if ( lane == -1 )
                printf ( "\nNo room available" ) ;
            else
            {
                add_dq ( &front[ lane ], &rear[ lane ], BOT, plate ) ;
                printf ( "\npark car at lane %d slot %d\n", lane, s ) ;
            }
        }
        else
        {
            if ( ad == 'D' )  /* departure of car */

            {
                for ( i = 0 ; i < 5 ; ++i )
                {
                    s = search ( front[i], plate ) ;
                    if ( s != -1 )
                    {
                        lane = i ;
                        break ;
                    }
                }
                if ( i == 5 )
                    printf ( "\nno such car!!\n" ) ;
                else
                {
                    printf ( "\ncar found at lane %d slot %d\n", lane, s ) ;
                    del_dq ( &front[ lane ], &rear[ lane ], s ) ;
                }
            }
            elseif ( ad == 'Q' )
                    exit ( 1 ) ;
        }
    }
}

/* adds a new element at the end of queue */
void add_dq ( struct node **f, struct node **r, int tb, char *p )
{
    struct node *q ;
    /* create new node */

    q = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
    strcpy ( q -> plate, p ) ;
    q -> link = NULL ;

    /* if the queue is empty */
if ( *f == NULL )
        *f = q ;
    else
    {
        if ( tb == BOT )
            ( *r ) -> link = q ;
        else
        {
            q -> link = *f ;
            *f = q ;
            return ;
        }
    }
    *r = q ;
}

char* del_dq ( struct node **f, struct node **r, int n )
{
    struct node *q, *top = NULL ;
    /* if queue is empty */
if ( *f == NULL )
        printf ( "queue is empty" ) ;
    else
    {
        if ( n == 0 )
        {
            strcpy ( temp, ( *f ) -> plate ) ;
            q = *f ;
            *f = ( *f ) -> link ;
            free ( q ) ;
            return temp ;
        }

        /* locate node */
for ( i = 0 ; i < n ; i++ )
        {
            /* drive out cars */

            push ( &top, ( *f ) -> plate ) ;

            /* delete the node */

            q = *f ;
            *f = q -> link ;
            free ( q ) ;
        }

        /* delete the nth node */

        q = *f ;
        *f = q -> link ;
        free ( q ) ;

        for ( i = 0 ; i < n ; i++ )
        {
            strcpy ( temp, pop ( &top ) ) ;

            /* add the node */

            add_dq ( f, r, TOP, temp ) ;
        }
    }
}

int count ( struct node *q )
{
    int c = 0 ;

    /* traverse the entire linked list */
while ( q!= NULL )
    {
        q = q -> link ;
        c++ ;
    }
    return c ;
}

int search ( struct node *q, char *p )
{
    int s = -1, c = 0 ;

    while ( q != NULL )
    {
        if ( strcmp ( p, q -> plate ) == 0 )
        {
            s = c ;
            break ;
        }
        else
        {
            q = q -> link ;
            c++ ;
        }
    }
    return ( s ) ;
}

/* adds a new element to the top of stack */
void push ( struct node **s, char* item )
{
    struct node *q ;
    q = ( struct node* ) malloc ( sizeof ( struct node ) ) ;
    strcpy ( q -> plate, item ) ;
    q -> link = *s ;
    *s = q ;
}

/* removes an element from top of stack */
char* pop ( struct node **s )
{
    struct node *q ;

    /* if stack is empty */
if ( *s == NULL )
    {
        return NULL ;
    }
    else
    {
        q = *s ;
        strcpy ( temp, q -> plate ) ;
        *s = q -> link ;
        free ( q ) ;
        return ( temp ) ;
    }
}

void q_display ( struct node *q )
{
    while( q != NULL )
    {
        printf ( "%s", q -> plate ) ;
        q = q -> link ;
    }
    printf ( "\n" ) ;
}

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

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

Code for Program to convert an expression in postfix form to an infix form in C Programming

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

#define MAX 50

struct postfix
{
    char stack[MAX][MAX], target[MAX] ;
    char temp1[2], temp2[2] ;
    char str1[MAX], str2[MAX], str3[MAX] ;
    int i, top ;
} ;

void initpostfix ( struct postfix * ) ;
void setexpr ( struct postfix *, char * ) ;
void push ( struct postfix *, char * ) ;
void pop ( struct postfix *, char * ) ;
void convert ( struct postfix * ) ;
void show ( struct postfix ) ;

void main( )
{
    struct postfix q ;
    char expr[MAX] ;

    clrscr( ) ;

    initpostfix ( &q ) ;

    printf ( "\nEnter an expression in postfix form: " ) ;
    gets ( expr ) ;

    setexpr ( &q, expr ) ;
    convert ( &q ) ;

    printf ( "\nThe infix expression is: " ) ;
    show ( q ) ;

    getch( ) ;
}

/* initializes data member */
void initpostfix ( struct postfix *p )
{
    p -> i = 0 ;
    p -> top = -1 ;
    strcpy ( p -> target, "" ) ;
}

/* copies given expression to target string */
void setexpr ( struct postfix *p, char *c )
{
    strcpy ( p -> target, c ) ;
}

/* adds an expr. to the stack */
void push ( struct postfix *p, char *str )
{
    if ( p -> top == MAX - 1 )
        printf ( "\nStack is full." ) ;
    else
    {
        p -> top++ ;
        strcpy ( p -> stack[p -> top], str ) ;
    }
}

/* pops an expr. from the stack */
void pop ( struct postfix *p, char *a )
{
    if ( p -> top == -1 )
        printf ( "\nStack  is empty." ) ;
    else
    {
        strcpy ( a, p -> stack[p -> top] ) ;
        p -> top-- ;
    }
}

/* converts given expr. to infix form */
void convert ( struct postfix *p )
{
    while ( p -> target[p -> i] )
    {
        /* skip whitespace, if any */
if( p -> target[p -> i] == ' ' )
            p -> i++ ;
        if ( p -> target[p -> i] == '%' || p -> target[p -> i] == '*' ||
             p -> target[p -> i] == '-' || p -> target[p -> i] == '+' ||
             p -> target[p -> i] == '/' || p -> target[p -> i] == '$' )
        {
            pop ( p, p -> str2 ) ;
            pop ( p, p -> str3 ) ;
            p -> temp1[0] = p -> target[p -> i] ;
            p -> temp1[1] = '\0' ;
            strcpy ( p -> str1, p -> str3 ) ;
            strcat ( p -> str1, p -> temp1 ) ;
            strcat ( p -> str1, p -> str2 ) ;
            push ( p, p -> str1 ) ;
        }
        else
        {
            p -> temp1[0] = p -> target[p -> i] ;
            p -> temp1[1] = '\0' ;
            strcpy ( p -> temp2, p -> temp1 ) ;
            push ( p, p -> temp2 ) ;
        }
        p -> i++ ;
    }
}

/* displays the expression */
void show ( struct postfix p )
{
    char *t ;
    t = p.stack[0] ;
    while ( *t )
    {
        printf ( "%c ", *t ) ;
        t++ ;
    }
}

Program to reconstruct a binary search tree.

Program to reconstruct a binary search tree.

Code for Program to reconstruct a binary search tree in C Programming


#include <stdio.h>
#include <conio.h>
#include <alloc.h>

#define MAX 101

struct node
{
    struct node *left ;
    int data ;
    struct node *right ;
} ;

void insert ( struct node **, int ) ;
void preorder ( struct node * ) ;
void postorder ( struct node * ) ;
void inorder ( struct node * ) ;
struct node * recons ( int *, int *, int ) ;
void deltree ( struct node * ) ;

intin[MAX], pre[MAX], x ;

void main( )
{
    struct node *t, *p, *q ;
    int req, i, num ;

    t = NULL ;  /* empty tree */


    clrscr( ) ;
    printf ( "Specify the number of items to be inserted: " ) ;
    while ( 1 )
    {
        scanf ( "%d", &req ) ;
        if ( req >= MAX || req <= 0 )
            printf ( "\nEnter number between 1 to 100.\n" ) ;
        elsebreak ;
    }

    for ( i = 0 ; i < req ; i++ )
    {
        printf ( "Enter the data: " ) ;
        scanf ( "%d", &num ) ;
        insert ( &t, num ) ;
    }

    printf ( "\nIn-order   Traversal:\n" ) ;
    x = 0 ;
    inorder ( t ) ;

    printf ( "\nPre-order  Traversal:\n" ) ;
    x = 0 ;
    preorder ( t ) ;

    printf ( "\nPost-order Traversal:\n" ) ;
    x = 0 ;
    postorder ( t ) ;

    deltree ( t ) ;
    t = NULL ;
    t = recons ( in, pre, req ) ;

    printf ( "\n\nAfter reconstruction of the binary tree.\n" ) ;

    x = 0 ;
    printf ( "\nIn-order   Traversal:\n" ) ;
    inorder ( t ) ;

    x = 0 ;
    printf ( "\nPre-order  Traversal:\n" ) ;
    preorder ( t ) ;
    x = 0 ;
    printf ( "\nPost-order Traversal:\n" ) ;
    postorder ( t ) ;

    deltree ( t ) ;
    getch( ) ;
}

/* inserts a new node in a binary search tree */
void insert ( struct node **sr, int num )
{
    if ( *sr == NULL )
    {
        *sr = ( struct node * ) malloc ( sizeof ( struct node ) ) ;

        ( *sr ) -> left = NULL ;
        ( *sr ) -> data = num ;
        ( *sr ) -> right = NULL ;
        return ;
    }
    else/* search the node to which new node will be attached */

    {
        /* if new data is less, traverse to left */
if ( num < ( *sr ) -> data )
            insert ( &( ( *sr ) -> left ), num ) ;
        else/* else traverse to right */

            insert ( &( ( *sr ) -> right ), num ) ;
    }
}

void preorder ( struct node *t )
{
    if ( t != NULL )
    {
        printf ( "%d\t", pre[x++]= t -> data ) ;
        preorder ( t -> left ) ;
        preorder ( t -> right ) ;
    }
}

void postorder ( struct node *t )
{
    if ( t != NULL )
    {
        postorder ( t -> left ) ;
        postorder ( t -> right ) ;
        printf ( "%d\t", t -> data ) ;
    }
}

void inorder ( struct node *t )
{
    if ( t != NULL )
    {
        inorder ( t -> left ) ;
        printf ( "%d\t", in[x++]= t -> data ) ;
        inorder ( t -> right ) ;
    }
}

struct node * recons ( int *inorder, int *preorder, int noofnodes )
{
    struct node *temp, *left, *right ;
    int tempin[100], temppre[100], i, j ;

    if ( noofnodes == 0 )
        return NULL ;

    temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
    temp -> data = preorder[0] ;
    temp -> left = NULL ;
    temp -> right = NULL ;

    if ( noofnodes == 1 )
        return temp ;
    for ( i = 0 ; inorder[i] != preorder[0] ; )
        i++ ;

    if ( i > 0 )
    {
        for ( j = 0 ; j <= i  ; j++ )
            tempin[j] = inorder[j] ;

        for ( j = 0 ; j < i  ; j++ )
            temppre[j] = preorder[j + 1] ;
    }

    left = recons ( tempin, temppre, i ) ;
    temp -> left = left ;

    if ( i < noofnodes - 1 )
    {
        for ( j = i ; j < noofnodes - 1 ; j++ )
        {
            tempin[j - i] = inorder[j + 1] ;
            temppre[j - i] = preorder[j + 1] ;
        }
    }

    right = recons ( tempin, temppre, noofnodes - i - 1 ) ;
    temp -> right = right ;

    return temp ;
}

void deltree ( struct node *t )
{
    if ( t != NULL )
    {
        deltree ( t -> left ) ;
        deltree ( t -> right ) ;
    }
    free ( t ) ;
}