Skip to content

Instantly share code, notes, and snippets.

@mbalayil
mbalayil / bit_count_shift.c
Created June 5, 2011 14:59
C program to count the number of bits set in an unsigned integer(shift operator)
/**
* Shift operators shift the bits in an integer variable by a specified
* number of positions. The << operator shifts bits to the left, and the >>
* operator shifts bits to the right. The syntax for these binary operators
* is x << n and x >> n
* Each operator shifts the bits in x by n positions in the specified
* direction. For a right shift, zeros are placed in the n high-order bits
* of the variable; for a left shift, zeros are placed in the n low-order
* bits of the variable.
* Here are a few examples:
@mbalayil
mbalayil / stack.c
Created May 28, 2011 11:05
Stack operations(basic) in C
/**
* A Stack is a last in, first out (LIFO) abstract data type and data
* structure.It is characterized by three fundamental operations: push,
* pop and stack top.
* PUSH : The push operation adds a new item to the top of the stack, or
* initializes the stack if it is empty, but if the stack is full and
* does not contain more space to accept the given item it is considered
* as an Overflow state (It means that the stack is overloaded or no more
* space for new item).
@mbalayil
mbalayil / del_array_element.c
Created May 18, 2011 11:40
To delete an element from an array
/**
* Program to delete an element from an array
**/
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int *array, size;
@mbalayil
mbalayil / substring.c
Created May 15, 2011 12:32
Check the occurrences of a pattern in a string
/**
* Program to check the occurrences of a pattern in a text/string
* This program checks whether the pattern occur in the string
* or not, if yes at what positions and also print the text/string
* highlighting the occurrences
**/
#include <stdio.h>
#include<string.h>
@mbalayil
mbalayil / palindrome.c
Created May 13, 2011 14:07
To check whether a string is a palindrome or not
/**
* A palindrome is a word, phrase, number or other sequence
* of units that can be read the same way in either direction
**/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
@mbalayil
mbalayil / factorial.c
Created May 13, 2011 11:02
To find the factorial of a number in C
/**
* To find the factorial of a number
**/
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int n, i, f;
@mbalayil
mbalayil / check_prime.c
Created May 13, 2011 06:08
To check whether a number is prime or not
/**
* To check whether a number is prime or not
**/
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int n, i, p = 1;
@mbalayil
mbalayil / swap_xor.c
Created May 8, 2011 19:05
Swapping without using temporary variable in C - XOR
/**
* Swapping without using a temporary variable in C
* (XOR)
**/
#include<stdio.h>
int main(void)
{
int a = 5; /* a = 0101 */
@mbalayil
mbalayil / swap_mul_div.c
Created May 8, 2011 18:48
Swapping without using temporary variable in C - Multiplication & Division
/**
* Swapping without using a temporary variable in C
* (Multiplication & Division)
**/
#include<stdio.h>
int main(void)
{
int a = 5, b = 10;
@mbalayil
mbalayil / binary_search.c
Created May 5, 2011 10:57
Binary Search in C
/** Binary Search locates the position of an item in a sorted list.
*
* Divide : It works by dividing the list into two halves and
* comparing the input value to be searched with the
* middle element of the list.
*
* Conquer : If the input value is equal to the mid value, search
* is successful. If greater, call binary search only
* on the greater sub-array of the sorted list and if
* lesser, call binary search on the lesser sub-array.