Monday, November 24, 2014

C using pointer



Count Number of Words using Pointer


#include
#include
#include
#include
#define low 1
#define high 0
 void main()
{
   int nob, now, nod, nov, nos, pos = high;
   char *str;
   nob = now = nod = nov = nos = 0;
   clrscr();

   printf("Enter any string : ");
   gets(str);
   while (*str != '\0')
    {
       if (*str == ' ')
       {
         pos = high;
         ++nob;
      }
      else if (pos == high)
       {
         pos = low;
         ++now;
      }
      if (isdigit(*str))
         ++nod;
      if (isalpha(*str))
         switch (*str)
           {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
            ++nov;
               break;
         }
         if (!isdigit(*str) && !isalpha(*str))
         ++nos;
      str++;
   }
   printf("\nNumber of words  %d", now);
   printf("\nNumber of spaces %d", nob);
   printf("\nNumber of vowels %d", nov);
   printf("\nNumber of digits %d", nod);
   printf20\nNumber of special characters %d", nos);
               getch();
}

Output

Enter any string : pritesh a taral from c4learn.com
Number of words 5
Number of spaces 4
Number of vowels 9
Number of digits 1
Number of special characters 5

C Program to Swap Two Numbers variables using Pointer


#include
#include
void swap(int*num1,in*num2)
{
int temp;
}
temp=*num1;
int main()
{
int num1,num2;
printf("\n Enter the first number: ");
scanf("%d",&num1);
printf("\n Enter the second number: ");
scanf("%d",&num2);
swap(&num1,&num2);
printf("\n First number :%d",num1);
printf("\n Second number:%d",num2);
return (0);
}

Output

Enter the first number:12
Enter the second number: 22
First number :22
Second Number:12

C Program to display array elements with addresses


#include
#include
#define size 10
int main()
{
   int a[3] = { 11, 22, 33 };
   printf("\n a[0] ,value=%d : address=%u", a[0], &a[0]);
   printf("\n a[1] ,value=%d : address=%u", a[1], &a[1]);
   printf("\n a[2] ,value=%d : address=%u", a[2], &a[2]);
   return (0);
}

Output

a[0] ,value=11 : address=2358832
a[1] ,value=22 : address=2358836
a[2] ,value=33 : address=2358840

C Program to perform binary search on array using recursion

#include
#include
#define size 10
int binsearch(int[], int, int, int);
int main()
  {
   int num, i, key, position;
   int low, high, list[size];
   printf("\nEnter the total number of elements");
   scanf("%d", &num);
   printf("\nEnter the elements of list :");
   for (i = 0; i < num; i++)
   {
      scanf("%d", &list[i]);
   }
   low = 0;
   high = num - 1;
   printf("\nEnter element to be searched : ");
   scanf("%d", &key);
   position = binsearch(list, key, low, high);
    if (position != -1)
   {
      printf("\nNumber present at %d", (position + 1));
    }
      else
      printf("\n The number is not present in the list");
   return (0);
   }
int binsearch(int a[], int x, int low, int high)
{
   int mid;
   if (low > high)
      return -1;
    mid = (low + high) / 2;
    if (x == a[mid])
    {
      return (mid);
    }
     else if (x < a[mid])
     {
      binsearch(a, x, low, mid - 1);
     }
      else
      {
      binsearch(a, x, mid + 1, high);
   }
}

Output


Enter the total number of elements : 5
Enter the elements of list : 11 22 33 44 55
Enter element to be searched : 33
Number present at 3

C Program to Implement Stack Operations Using Array


#include
#include
#include
#define size 5
struct stack
{
   int s[size];
   int top;
} st;
 int stfull()
 {
   if (st.top >= size - 1)
      return 1;
   else
      return 0;
}
 void push(int item)
{
   st.top++;
   st.s[st.top] = item;
}
 int stempty()
 {
   if (st.top == -1)
      return 1;
   else
      return 0;
}
 int pop()
{
   int item;
   item = st.s[st.top];
   st.top--;
   return (item);
}
void display()
{
   int i;
   if (stempty())
      printf("\nStack Is Empty!");
   else {
      for (i = st.top; i >= 0; i--)
         printf("\n%d", st.s[i]);
   }
}
 int main()
{
   int item, choice;
   char ans;
   st.top = -1;
   printf("\n\tImplementation Of Stack");
   do
     {
      printf("\nMain Menu");
      printf("\n1.Push \n2.Pop \n3.Display \n4.exit");
      printf("\nEnter Your Choice");
      scanf("%d", &choice);
      switch (choice)
     {
      case 1:
         printf("\nEnter The item to be pushed");
         scanf("%d", &item);
         if (stfull())
            printf("\nStack is Full!");
         else
            push(item);
         break;
      case 2:
         if (stempty())
            printf("\nEmpty stack!Underflow !!");
         else
          {
            item = pop();
            printf("\nThe popped element is %d", item);
         }
         break;
      case 3:
         display();
         break;
      case 4:
         exit(0);
      }
      printf("\nDo You want To Continue?");
      ans = getche();
   }
    while (ans == 'Y' || ans == 'y');
    return 0;
}

No comments:

Post a Comment