Search This Blog

Translate This Page

Friday, 13 September 2013

Print Reverse of a string using Recursion


Print reverse of a  string using recursion:


# include <stdio.h>

/* Function to print reverse of the passed string */
void reverse(char *str)
{
   if(*str)
   {
       reverse(str+1);
       printf("%c", *str);
   }
}

int main() 
{
   char a[] = "Tech Stuff for Interview";
   reverse(a);
   return 0;
}

Explanation:
 Recursive function (reverse) takes string pointer (str) as input and calls itself with next location to passed pointer (str+1). Recursion continues this way, when pointer reaches ‘\0′, all functions accumulated in stack print char at passed location (str) and return one by one.


Friday, 30 August 2013

Programme for getting IP address information

# C Programme for Getting IP Address Information Using Network Interfacing

#include <stdio.h>
#include <string.h> /* for strncpy */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>

int main()
{
 int fd;
 struct ifreq ifr;

 fd = socket(AF_INET, SOCK_DGRAM, 0);

 /* I want to get an IPv4 IP address */
 ifr.ifr_addr.sa_family = AF_INET;

 /* I want IP address attached to "eth0" */
 strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);

 ioctl(fd, SIOCGIFADDR, &ifr);

 close(fd);

 /* display result */
 printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

 return 0;
}

Multiline macros in C

Multi Line Macro in C


We can write multi line macro same like function but each statement ends with "\". Below is the simple macro , which accepts input number from user, and prints whether entered number is even or odd.

#include <stdio.h>

#define MACRO(num, str) ({\
            printf("%d", num);\
            printf(" is");\
            printf(" %s number", str);\
            printf("\n");\
           })

int main(void)
{
    int num;
    printf ("Enter no\n");
    scanf("%d", &num);

    if (num & 1)
        MACRO(num, "Odd");
    else
        MACRO(num, "Even");

    return 0;
}


Output:

#cc     macro.c   -o macro
#./macro

#Enter a number: 10

#10 is Even number

Friday, 1 March 2013

How to create Zombie Process IN Linux


Zombie Process:

On Unix and Unix-like computer operating systems, a Zombie process or defunct process is a process that has completed execution but still has an entry in the process table. This entry is still needed to allow the parent process to read its child's exit status

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
int id ,x ,y;
id=fork();
if(id==0)
{
x=getpid();
y=getppid();            // in this progtamme first parent execute then child. in parent sleeping for 15ms and child for 5                          ms. in 5 ms child process finish so it create zombie process
printf("Child process 1>PID:%d child 2>PPID:%d\n",x,y);
sleep(5);
printf("\nchild terminated\n");
}
else{
x=getpid();
y=getppid();
printf("Parent process 1>PID:%d parent 2>PPID:%d\n",x,y);
sleep(15);
//wait(&id);
printf("\nparent terminated\n");
}
return(0);
}


Output:


Parent process 1>PID:5307 parent 2>PPID:2588
Child process 1>PID:5308 child 2>PPID:5307

child terminated

parent terminated



Wednesday, 27 February 2013

Rotate bits of a Numbers


Left and Right Rotation of BIT:     


#include <stdio.h>
#define BIT 32
int leftrotate(int n,int d)
{
        return (n<<d)|(n>>(BIT-d));
}
int rightrotate(int n,int d)
{
        return (n>>d)|(n<<(BIT-d));
}
int main()
{
        int n,d;
        printf ("Enter no\n");
        scanf ("%d",&n);
        printf ("how many bit left rotate\n");
        scanf ("%d",&d);
        printf (" left rotation of %d by %d is ",n,d);
        printf ("%d\n",leftrotate(n,d));
        printf (" right rotation of %d by %d is ",n,d);
        printf ("%d\n",rightrotate(n,d));
}

output:

Enter no
4
how many bit left rotate
2
 left rotation of 4 by 2 is 16
 right rotation of 4 by 2 is 1

Print Reverse of a string using Recursion


Print reverse of a  string using recursion:


# include <stdio.h>

/* Function to print reverse of the passed string */
void reverse(char *str)
{
   if(*str)
   {
       reverse(str+1);
       printf("%c", *str);
   }
}

int main() 
{
   char a[] = "Tech Stuff for Interview";
   reverse(a);
   return 0;
}



C program to print all permutations of a given string



C program to print all permutations of a given string:



# include <stdio.h>
void swap (char *x, char *y)                //swap values at two pointers
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void permute(char *a, int i, int n)

{
   int j;
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j));                 //backtrack
       }
   }
}

int main()
{
   char a[] = "ABC";
   permute(a, 0, 2);
   return 0;
}

ASCII Table

Add caption
  

Implement Your Own sizeof Function

Implement Your Own sizeof () function:


#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
int main()
{
    double x;
    printf("%d", my_sizeof(x));
    getchar();
    return 0;
}



Programme to find Nearest Vowel


This code is for finding nearest Vowel whatever Character you enter by your keyboard.

#include <stdio.h>
int main()
{
        int a[10]={65,69,73,79,85,97,101,105,111,117};  //Array of Vowel (a,e,i,o,u,A,E,I,O,U)
        char ch;
        int temp,temp1,i;
        printf ("enter character\n");
        scanf ("%c",&ch);
        temp=ch-a[0];
        for (i=0;i<10&&((ch-a[i])>=0);i++)
        {
                if( ch==a[i] )
                        {
                        printf ("nearest vowel is:  %c\n",a[i]);
                        return 0;
                        }
                if( temp>(ch-a[i]) )
                        {
                        temp1=a[i];
                        }

        }
        printf ("nearest vowel is %c\n",temp1);
        return 0;
}

Tuesday, 26 February 2013

Subtracting two numbers without using '-' operator

Subtracting two numbers without using '-' operator


#include <stdio.h>

int add(int a,int b)
{
int x;
x=a^b;
while(a&b)
{
b=((a&b)<<1);
a=x;
x=a^b;
//b=(a^b);
}
return x;
}
int sub(int a, int b) // add a with b's 2's complement.
{
return (add(a, add(~b, 1)));
}
int main() {
int a, b, res;
a = 3, b = 1;
printf ("Enter two no\n");
scanf ("%d %d",&a,&b);
res = sub(a, b);
printf("%d\n", res);
printf ("After Sub. is  no  %d",res);
return 0;
} 


output: 
Enter two no
10
5
After Sub. no is 5

Add two numbers without using arithmetic operators

Add two numbers without using arithmetic operators:


Method 1:

#include<stdio.h>
int Add(int x, int y)
{
    // Iterate till there is no carry  
    while (y != 0)
    {
        // carry now contains common set bits of x and y
        int carry = x & y;  

        // Sum of bits of x and y where at least one of the bits is not set
        x = x ^ y; 

        // Carry is shifted by one so that adding it to x gives the required sum
        y = carry << 1;
    }
    return x;
}

int main()
{
    printf("%d", Add(15, 32));
    return 0;
}


Method 2:  (Using for Loop)



int sum(int num1, int num2)
{
    int total, temp, temptotal;  
    total = num1 ^ num2;    
  
    for(temp = num1 & num2; temp != 0;temp = temptotal & temp) 
      {      
      temptotal = total;    
      total = total ^ (temp = temp << 1);  
      }
  
   return total;
}


Latest Current Visitor

World Clock Time, for View Click on Country