Friday, April 8, 2011

#C_CODE: Integer to Ascii conversion

#include <stdio.h>
#include <string.h>
void reverse(char *s)
{
 int len = strlen(s) - 1;
 int i;
 for(i=0;i < strlen(s)/2; i++)
 {
  s[i]   ^= s[len];
  s[len] ^= s[i];
  s[i]   ^= s[len--];
 }
}

char *intoa(int num)
{
 static char buff[10];
 char *s = &buff[0];
 int sign;

 if((sign = num) < 0)
 {
  num = -num; //Make num positive  
 }

 do
 {
  *(s++) = (num%10) + '0';
 }while(num /= 10);
 
 if(sign < 0)
  *(s++) = '-';

 *(s) = '\0';
 reverse(&buff[0]);
 return (&buff[0]);
}

int main()
{
 int num,i;
 char *s;

 printf("Enter the num: ");
 scanf("%d",&num);
 s = intoa(num);
 printf("\nThe num %d in ascii is: ",num);
 for(i=0; i < strlen(s); i++)
  printf("\n%c",s[i]);

 fflush(stdin);
 getchar();
}

REF: C Programming Language-K&R

#C_CODE: Reverse String

#include <stdio.h>
#include <string.h>
 
void reverse(char *s)
{
 int len = strlen(s)-1;
 int i;
 for(i=0; i < strlen(s)/2; i++)
 {
  s[i]   ^= s[len];
  s[len] ^= s[i];
  s[i]   ^= s[len--];
 }
}

int main(void)
{
 char buff[35];
 fputs("Input string: ",stdout);
 fgets(buff,sizeof(buff),stdin);
 fflush(stdin);
 *(buff + strlen(buff) -1 ) = '\0';

 reverse(buff);
 printf("\n The reversed string is: %s\n",buff);
 getchar();
}

REF: C Programming Language-K&R

#C_CODE: Binary Search

#include <stdio.h>
 
void InsertionSort(int *ptr,int size)
{
 int i,j,index;
 for(i=1;i {
  index = ptr[i];
  for(j=i; j>0&&ptr[j-1] >index; j--)
   ptr[j] = ptr[j-1];

  ptr[j] = index;
 }
}

int BinarySearch(int *ptr,int key,int size)
{
 int low,mid,high;
 low = 0;
 high = size-1;
 while(low <= high)
 {
  mid = (low + high)/2;
  if(key < ptr[mid])
   high = mid - 1;
  else if(key > ptr[mid])
   low = mid + 1;
  else
   return mid;
 }
 return -1;
}

int main()
{
 int i;
 int a[] = {5,7,0,8,4,9,6,1,3,2};
 int res;
 
 InsertionSort(a,10);
 printf("\nInsertion sort: ");
 for(i=0;i<10;i++)
  printf("%d ",a[i]);
 
 for(i=0; i<10;i++)
 {
  res = BinarySearch(a,i,10);
  if(res != -1)
   printf("\nThe value %d is found in position: %d\n",i,res);
 }

 fflush(stdin);
 getchar();
}

REF: C Programming Language-K&R

#C_CODE: Sorting Algorithms

#include <stdio.h>
 
void BubbleSort(int *ptr, int size)
{
 int i, j;
 for(i=0; i < size-1; i++)
 {
  for(j=0; j < size-1-i; j++)
  {
   if(ptr[j] > ptr[j+1])
   {
    ptr[j]   ^= ptr[j+1];
    ptr[j+1] ^= ptr[j];
    ptr[j]   ^= ptr[j+1];
   }
  }
 }

}

void SelectionSort(int *ptr, int size)
{
 int i,j,min;
 for(i=0; i < size-1;i++)
 {
  min = i;
  for(j=i+1;j < size;j++)
  {
   if(ptr[j] < ptr[min])
    min = j;
  }
  ptr[i]   ^= ptr[min];
  ptr[min] ^= ptr[i];
  ptr[i]   ^= ptr[min];
 }
}

void InsertionSort(int *ptr,int size)
{
 int i,j,index;

 for(i=1; i < size;i++)
 {
  index = ptr[i];
  for(j=i; j>0 && ptr[j-1] > index; j--)
   ptr[j] = ptr[j-1];
  ptr[j] = index;
 }
}

int main()
{
 int i;
 int a[] = {5,7,0,8,4,9,6,1,3,2};
 int b[] = {5,7,0,8,4,9,6,1,3,2};
 int c[] = {5,7,0,8,4,9,6,1,3,2};

 BubbleSort(a,10);
 printf("\nBubble sort: ");
 for(i=0;i<10;i++)
  printf("%d ",a[i]);

 SelectionSort(b,10);
 printf("\nSelection sort: ");
 for(i=0;i<10;i++)
  printf("%d ",a[i]);

 InsertionSort(c,10);
 printf("\nInsertion sort: ");
 for(i=0;i<10;i++)
  printf("%d ",a[i]);
 
 fflush(stdin);
 getchar();
}

Ref: blog.Codebeach.com

#C_CODE: Bit Set Count

#include <stdio.h>
 
int bitsetcount(int num)
{
 int i;
 for(i=0; num; i++)
  num &= num - 1;
 return i;
}

int main()
{
 int num;
 printf("Enter the num: ");
 scanf("%d",&num);
 printf("The no. of bits set in %d is: %d\n",num,bitsetcount(num));
 fflush(stdin);
 getchar();
}

Ref: C Programming Language - K&R

#C_CODE: Clear Bits

#include <stdio.h>
 
int main()
{
 int num,bits,pos,res;

 printf("Enter the num: ");
 scanf("%d",&num);
 printf("\nEnter the no. of bits to be cleared: ");
 scanf("%d",&bits);
 printf("\nEnter the pos(0 onwards): ");
 scanf("%d",&pos);
    
 printf("The result: %d\n", 
( num & (~((~(~0 << bits)) << pos + 1 - bits)) ) );
 fflush(stdin);
 getchar(); 
}

Example: num = 255(0b1111,1111), bits = 3, pos = 5, Result = 199(0b11000111)


Ref: C Programming Language - K&R

#C_CODE: Extract bits

#include <stdio.h>
 
int main()
{
 int num,bits,pos,res;

 printf("Enter the num: ");
 scanf("%d",&num);
 printf("\nEnter the no. of bits to be extracted: ");
 scanf("%d",&bits);
 printf("\nEnter the pos(0 onwards): ");
 scanf("%d",&pos);
    
 res = (num >> (pos + 1 - bits)) & (~(~0 << bits));
 printf("\nThe extracted bits are: %d",res  );
 fflush(stdin);
 getchar();
}


EXAMPLE: num = 233(0b11101001), pos = 5, bits = 3, Result = 5(0b101)


Ref: C Programming Language - K&R

#C_CODE: squeeze(s,x) - Removes all occurances of character x from string s

/* Include files */
#include <stdio.h>
#include <string.h>
 

void squeeze(char *s,char x)
{
 int i,j;

 for(i=j=0; s[i] != '\0'; i++)
  if(s[i] != x)
   s[j++] = s[i];
 s[j] = '\0';
}

int main()
{
 char buff[35];

 fputs("Input string: ",stdout);
 fgets(buff,sizeof(buff),stdin);
 fflush(stdin);
 *(buff + strlen(buff) - 1) = '\0';

 squeeze(buff,'a');
 printf("The output: %s\n",buff);
 getchar();
}


Ref: C Programming Language - K&R

Thursday, April 7, 2011

Leap Year

A year is a leap year, if it is divisible by 4 but not by 100, except that years divisible by 400 are leap years.

if( ((year % 4 == 0)&&(year % 100 != 0)) || (year % 400 == 0) )
printf("%d is a leap year\n",year);
else
printf("%d is not a leap year\n",year);


REF: C Programming Language
Brian W. Kernighan &
Dennis M. Ritchie