Monday, 20 January 2014

Character Array(String) Programs

 Character Array(String) Programs

Program -1  : Array of characters
// compile time initialization

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

void main()
{
char nm[]={ 's','h','i','v','\0'};
char name[]="Parvati";
          int i;
          clrscr();
          printf("\n Name : %s",nm);
          printf("\n Name1 : %s",name);
          for(i=0;i<5;i++)
          {
                   printf("\n  %5c",nm[i]);
          }
          getch();
}


Program -2  : Write a program to create triangle of given string
/* character triangle
S
S h
S h i
S h i v
*/
#include<conio.h>
#include<stdio.h>

void main()
{
          char nm[20];
          int i,j;
          clrscr();
          printf("\n Enter your name : ");
          gets(nm);
          for(i=0;nm[i]!=NULL;i++)
          {
                   for(j=0;j<=i;j++)
                   {
                             printf(" %c",nm[j]);
                   }
                   printf("\n");
          }
          getch();
}

Program -3  : Write a program to calculate length of String without using any Library function.

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

void main()
{
          char nm[100];
          int i,l=0;
          clrscr();
          printf("\n Enter nay string : ");
          gets(nm);

          // find length
          for(i=0;nm[i]!=NULL;i++)
          {
                   l++;
          }
          printf("\n Length = %d",l);
          // print each character
          for(i=0;i<l;i++)
          {
                   printf("\n %c",nm[i]);

          }

          getch();
}


Program -4  : Write a program to count no. of vowels and consonant from given string.

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

void main()
{
          char nm[20];
          int i,v=0,c=0;
          clrscr();

          printf("\n Enter any string : " );
          gets(nm);

          for(i=0;nm[i]!=NULL;i++)
          {
          if(isalpha(nm[i]))
            {
            if(nm[i]=='a' || nm[i]=='e' || nm[i]=='i' || nm[i]=='o' || nm[i]=='u'|| nm[i]=='A' || nm[i]=='E' || nm[i]=='I' || nm[i]=='O' || nm[i]=='U')
                             v++;
           else
                             c++;
          }
          }
          printf("\n Vowels = %d",v);
          printf("\n Consonents = %d",c);
          getch();
}



Program 5  : Write a program to copy one string to other string without using liberary function.

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

void main()
{
          char s1[30],s2[30];
          int l=0,i;
          clrscr();
          printf("\n Enter any string : ");
          gets(s1);
          while(s1[l]!='\0')
          {
                   l++;
          }
          for(i=0;i<=l;i++)
          {
                   s2[i]=s1[i];
          }
          s2[i+1]='\0';
          printf("\n Copy = %s",s2);
          getch();
}


Program-6 :  Write a program to count total number of words from given string and display each word in new line.

 Ex :  I like C Language 
I
Like
C
Language
Total words = 4

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

void main()
{
          char s[100];
          int l=0,i,c=1;
          clrscr();
          printf("\n Enter any string : ");
          gets(s);
          while(s[l]!='\0')
          {
                   l++;
          }
          printf("\n");
          for(i=0;i<l;i++)
          {
                   printf("%c",s[i]);
                   if(s[i]==' '&& s[i+1]!=' ')
                   {
                             c++;
                             printf("\n");
                   }
          }
printf("\n Total No. Of Words = %d",c);
          getch();
}


Program-7 :  Write a program to reverse given string.
Ex :
Original string  : Shiv
Reverse String : vihS

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

void main()
{
          char s1[30],s2[30];
          int l=0,i,j=0;
          clrscr();
          printf("\n Enter any string : ");
          gets(s1);
          while(s1[l]!='\0')
          {
                   l++;
          }
          printf("\n");
          for(i=l-1;i>=0;i--)
          {
                   s2[j]=s1[i];
                   j++;
          }
          s2[j]='\0';
printf("\n Original String = %s",s1);
printf("\n Reverse String  = %s",s2);

getch();
}


Program-8 :  Write a program to check whether given string is palindrome or not.
Ex:
String : nayan
This is palindrome

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

void main()
{
          char s1[30],s2[30];
          int l=0,i,j=0,flag=0;
          clrscr();
          printf("\n Enter any string : ");
          gets(s1);
          while(s1[l]!='\0')
          {
                   l++;
          }
          printf("\n");
          for(i=l-1;i>=0;i--)
          {
                   s2[j]=s1[i];
                   j++;
          }
          s2[j]='\0';
          printf("\n Original String = %s",s1);
          printf("\n Reverse String  = %s",s2);

// logic to check palindrome string

          for(i=0;i<l;i++)
          {
                   if(s1[i]!=s2[i])
                   {
                             flag=1;
                             break;
                   }
          }
          if(flag==1)
                   printf("\n String is not palindrome");
          else
                   printf("\n String is palindrome");
          getch();
}



Program-9 :  Write a program to convert all lowercase letter  to uppercase letter of given string.

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

void main()
{
          char s[30];
          int l=0,i,c=1;
          clrscr();
          printf("\n Enter any string : ");
          gets(s);
          while(s[l]!='\0')
          {
                   l++;
          }
          printf("\n");
          for(i=0;i<l;i++)
          {
                   if(s[i]>='a' && s[i]<='z')
                   {
                             s[i]=s[i]-32;
                   }
          }
          printf("\n Name = %s",s);
          getch();
}


Program-10 :  Write a program to convert all uppercase letter to lowercase
And  lowercase letter to uppercase of the given string.

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

void main()
{
          char s[30];
          int l=0,i,c=1;
          clrscr();
          printf("\fn Enter any string : ");
          gets(s);
          while(s[l]!='\0')
          {
                   l++;
          }
          printf("\n");
          for(i=0;i<l;i++)
          {
                   if(s[i]>='a' && s[i]<='z')
                   {
                             s[i]=s[i]-32;
                   }
                   else if(s[i]>='A' && s[i]<='Z')
                   {
                             s[i]=s[i]+32;
                   }
                   else
                   {}
          }
          printf("\n Name = %s",s);
          getch();
}

Posted By :Ruchita Pandya

Saturday, 16 March 2013

Program - Shell sort


Program - Shell sort

// Shell  sort

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

void shell_sort(int[],int);
void output(int [],int);

void main()
{
     int values[50],n,i,j,temp;
     clrscr();

     printf("How Many Elements You Want To Enter ?  :");
     scanf("%d",&n);

     for(i=0;i<n;i++)
       {
 printf("\n Enter any number : ");
 scanf("%d",&values[i]);
       }

       printf("\n List before sorting");
       output(values,n);
             
       shell_sort(values,n);

       printf("\n list after sorting ");
       output(values,n);
       getch();
}
void output(int values[],int n)
{
int i;
for(i=0;i<n;i++)
{
 printf("\n  %d",values[i]);
}

}
void shell_sort(int array[],int size)
{
  int temp,gap,i,swap;

  gap=size/2;

  do
   {

     do
       {
        swap=0;
  
  
      for(i=0;i<size-gap;i++)
        {

if(array[i]>array[i+gap])
           {
                   temp=array[i];
                   array[i]=array[i+gap];
                   array[i+gap]=temp;
                   swap=1;
           }

    }
}while(swap);
  }while(gap=gap/2);
}

Program - Insertion Sort


Program - Insertion Sort 

// Insertion Sort

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

void insertion_sort(int[],int);
void output(int[],int);

void main()
{
          int values[50],i,n;
          clrscr();

          printf("\n How many elements you want to Enter ? : ");
          scanf("%d",&n);

          for(i=1;i<=n;i++)
          {
                   printf("\n Enter any number : ");
                   scanf("%d",&values[i]);
          }

          printf("\n List before sorting");
          output(values,n);

          insertion_sort(values,n);
          printf("\n List after sorting");
          output(values,n);
          getch();
}

void output(int l[],int n)
{
          int i;

          for(i=1;i<=n;i++)
          {
                   printf("\n  %d",l[i]);
          }
}





void insertion_sort(int l[],int n)
{
          int temp,i,pointer;
          l[0]=0;
          for(i=1;i<=n;i++)
          {
            temp=l[i];
            pointer=i-1;

            while(temp<l[pointer])
             {
                   l[pointer+1]=l[pointer];
                   pointer--;
             }
             l[pointer+1]=temp;
          }
}

Program - Priority Queue

Program - Priority Queue
// Priority queue

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

#define MAX 5

// Structure for data

struct data
{
  int val,p,o;
}d[MAX];

 int front=-1,rear=-1;

// Insert operation
void  insert(struct data d1)
{
   struct data temp;
   int i,j;

   if(rear==MAX-1)
  {
    printf("\n Priority Queue is Full...");
    getch();
  }
  else
  {
    rear++;
    d[rear]=d1;
    if(front==-1)
      front=0;
    for(i=front;i<=rear;i++) // arrange priority 
    {
      for(j=i+1;j<=rear;j++)
      {
if(d[i].p > d[j].p)
{
 temp=d[i];
 d[i]=d[j];
 d[j]=temp;
}
else
{
 if(d[i].p==d[j].p) // if two job has same priority
 {
   if(d[i].o > d[j].o) // arrange as per order from
   {
     temp=d[i];
     d[i]=d[j];
     d[j]=temp;
   }
 }
}
      }
    }
  }
}
// Delete operation
void  deletion()
{
  struct data d1;
  if(front==-1)
    printf("\n Priority Queue is Empty...");
  else
  {
    printf("-------------------------------\n");
    printf("\n Value:    %d",d[front].val);
    printf("\n Priority: %d",d[front].p);
    printf("\n Order:    %d",d[front].o);
    printf("\n -------------------------------");
    if(front==rear)
      front=rear=-1;
    else
      front++;
  }
}

// Display operation
void  display()
{
   int i;

  if(front==-1)
    printf("\n Priority Queue is Empty...");
  else
  {
    for(i=front;i<=rear;i++)
    {
      printf("\n -------------------------------");
      printf("\n Object :  %d",i+1);;
      printf("\n Value  :  %d",d[i].val);
      printf("\n Priority: %d",d[i].p);
      printf("\n Order  :  %d",d[i].o);
      printf("\n -------------------------------");
    }
  }
}

// main() section
void main()
{
  struct data d1;
  // Executable section
  do
  {
    int ch;
    clrscr();
    // Print menu
    printf("\n-------------------");
    printf("\n     M E N U             ");
    printf("\n-------------------");
    printf("\n1 Insertion");
    printf("\n2 Deletion");
    printf("\n3 Display");
    printf("\n4 Exit");
    printf("\n-------------------");
    printf("\nEnter Choice... ");
    scanf("%d",&ch);
    switch(ch)
    {
       // Insertion case
       case 1:
printf("\n\t\tEnter Value:- ");
scanf("%d",&d1.val);
printf("\n\t\tEnter Priority:- ");
scanf("%d",&d1.p);
printf("\n\t\tEnter Order:- ");
scanf("%d",&d1.o);
insert(d1);
break;
       // Deletion case
       case 2:
deletion();
getch();
break;
       // Display case
       case 3:
display();
getch();
break;
       // Exit case
       case 4:
exit(0);
       // Default case
       default:
printf("\nInvalid Choice...");
getch();
    }
  }while(1);

  getch();
}

Posted By : Ruchita Pandya

Monday, 2 April 2012

Program : Addition of two polynomial equation


Program : Addition of two polynomial equation

// Add two  polynomial equation using linked list

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

struct link
{
          int coef;
          int exp;
          struct link *next;
};


struct link *node,*start,*new1,*pre;
int i;

void create_poly(struct link *node);
void display(struct link *node);
void add_poly(struct link *node);

void main()
{
          clrscr();
          printf("\n create polynomial equation");
          node=(struct link *)malloc(sizeof(struct link));
          create_poly(node);
          printf("\n Polynomial equation is as follow\n");
          display(node);
          add_poly(node);
          printf("\n after addition polynomial equation :");
          display(node);
          getch();
}

void create_poly(struct link *node)
{
          char ans;
          i=0;
          start->next=NULL;
          node=start;
          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;
                   printf("\n Enter Coeficient value : ");
                   scanf("%d",&node->coef);
                   printf("\n Enter exponent value   : ");
                   scanf("%d",&node->exp);

                   node->next=NULL;

                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
}

void display(struct link *node)
{
          node=start->next;
          while(node)
          {
                   printf("%dX ^ %d  ",node->coef,node->exp);
                   node=node->next;
                   if(node!=NULL)
                     printf(" + ");
          }

}

void add_poly(struct link *node)
 {
    int t_coef,t_exp;
    char ans;
    struct link *temp,*tp;
    node=start->next;
    pre=start;
    printf("\n Enter 'n' for break:-");
    ans=getchar();
          while(ans!='n')
          {
                   printf("\n Enter Coeficient value : ");
                   scanf("%d",&t_coef);
                   printf("\n Enter exponent value   : ");
                   scanf("%d",&t_exp);

                   pre=start;
                   node=start->next;

                   while(node)
                    {
                      if(t_exp > node->exp)
                       {
                         new1=(struct link *)malloc(sizeof(struct link ));
                         new1->next=node;
                         pre-> next=new1;
                         new1->coef=t_coef;
                         new1->exp=t_exp;
                         break;
                       }
                    else
                    {
                      if(t_exp == node->exp)
                             {
                                node->coef=node->coef+t_coef;
                                node->exp=t_exp;
                                break;
                             }
                    }

                    pre=pre->next;
                    node=node->next;
                 }
             fflush(stdin);
             printf("\n Enter 'n' for break:-");
             ans=getchar();
     }
  }

Posted By :Ruchita Pandya

Program : Create Polynomial equation using Linked List


Program : Create Polynomial equation  using Linked List

Polynomial equation  
5x^3 +4x^2+6x^1

// Create polynomial equation using linked list

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

struct link
{
          int coef;
          int exp;
          struct link *next;
};

struct link *node,*start;
int i;

void create_poly(struct link *node);
void display(struct link *node);

void main()
{
          clrscr();
          printf("\n create polynomial equation");
          node=(struct link *)malloc(sizeof(struct link));
          create_poly(node);
          printf("\n Output\n");
          display(node);
          getch();
}

void create_poly(struct link *node)
{
          char ans;
          i=0;
          start->next=NULL;
          node=start;
          fflush(stdin);
          printf("\n Enter 'n' for break:-");
          ans=getchar();
          while(ans!='n')
          {
                   node->next=(struct link *)malloc(sizeof(struct link));
                   node=node->next;
                   printf("\n Enter Coeficient value : ");
                   scanf("%d",&node->coef);
                   printf("\n Enter exponent value   : ");
                   scanf("%d",&node->exp);

                   node->next=NULL;

                   fflush(stdin);
                   printf("\n Enter 'n' for break:-");
                   ans=getchar();
                   i++;
          }
}

void display(struct link *node)
{
          node=start->next;
          while(node)
          {
                   printf("%dX ^ %d",node->coef,node->exp);
                   node=node->next;
                   if(node!=NULL)
                     printf(" + ");
          }

}

Posted By  : Ruchita Pandya