Thursday, September 12, 2013

Swapping the bits of an integer using c (compile using gcc filename.c -lm)

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


void showbit(int h);

int main()
{   
    int no,temp,temp1;
    double a,b;
   
   
    printf("\nEnter the no.:\t");
    scanf("%d",&no);

    showbit(no);
    printf("\nEnter the bit index :  ");
    scanf("%lf",&a);
    printf("\nEnter the bit index :  ");
    scanf("%lf",&b);
   
    a=pow(2.0,a);
    temp=no&(int)a;
    (temp==0)?(temp=0):(temp=1); //checking whether the specified bit is 0 or 1

    b=pow(2.0,b);
    temp1=no&(int)b;
    (temp1==0)?(temp1=0):(temp1=1);



    if(temp1==temp) // if both the bits are same no need to swap
    {
    showbit(no);
    exit(0);
    }   
    else
    {

    no=no^(int)a; // if bits are different toggle them
    no=no^(int)b;

    }
    showbit(no);
   
    return 0;
}       
   
void showbit(int h) //displaying the bits of a number
{
    int l;
     int i,j,k[32];
    i=1;
    l=h&i;
    (l==0)?(k[0]=0):(k[0]=1);
   
    for(j=1;j<32;j++)
    {
    i*=2;
    l=h&i;
    (l==0)?(k[j]=0):(k[j]=1);
    }
    for(j=31;j>=0;j--)
    printf("%d",k[j]);

    printf("\n");

}

Grep implementation

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <malloc.h>

/* supply argv[1] the string to be searched , supply argv[2] the DIR name */


void grepfn(char str1[],char search[], char filename1[]);

void filesearch(char filename[], char find1[]);

int main(int argc, char *argv[])
{


int chk,len;
char *filename,*file1;
char *find;

len=strlen(argv[1]);
find=(char*)malloc(sizeof(char)*len);
strcpy(find,argv[1]);

DIR *dir= NULL;
struct dirent *file = NULL;


dir= opendir(argv[2]);

while(file=readdir(dir))
{

len= sizeof(file->d_name);
filename=(char*)malloc(sizeof(char)*len);

strcpy(filename,file->d_name);

chk=check(filename);

if(chk)
{
file1=(char *)malloc(((sizeof(char))*strlen(argv[2]))+((sizeof(char))*strlen(filename)));
sprintf(file1,"%s/%s",argv[2],filename);
filesearch(file1,find);
}
else
{
continue; //skip to next file if the file has .o  extension
}

}
}

int check(char filename[]) // function to check whether the file contains .o
{
int i=0;

while(filename[i]!='.'&&filename[i]!='\0')
{
i++;
}
if(filename[i]=='.'&&filename[i]!='\0')
i++;

while(filename[i]!='\0')
{
if(filename[i]=='o')
return 0;

i++;
}

return 1;
}

void filesearch(char filename[],char find1[])
{
int i=0, cnt=0,k=0, line=0;
char ch;
char str[300];

FILE *fd = NULL;
    fd = fopen(filename,"r");

    if(NULL == fd)
    {
        printf("\n fopen() Error!!!\n");
        exit(0);
    }


ch=(char)fgetc(fd);
i=0;
        while(ch!= EOF)// this loop for counting the number of lines in the file
{
ch=(char)fgetc(fd);
if(ch=='\n')
{
cnt++;
}


}

fseek( fd, 0, SEEK_SET );//restoring the pointer to begining of the file

ch=(char)fgetc(fd);

for(line=0;line<cnt;line++)
{
while(ch!='\n')// this loop copies the line to str[]
{
str[i]=ch;
ch=(char)fgetc(fd);
i++;
}
str[i]='\0';

grepfn(str,find1,filename); // passing the line (str), search pattern(find1), and name of the file(filename)
ch=(char)fgetc(fd);
i=0;
}

fclose(fd);


}

void grepfn(char str1[], char search[], char filename1[]) //searches the pattern in a line
{
int flag=0,j=0,k=0,size,size1;

size = strlen(str1);
size1 = strlen(search);

for(k=0;k<size;k++)//loop for pattern matching
{
if(search[j]==str1[k])
{
j++;
}
else
{
j=0;
continue;
}
if(j==size1) // updates flag = 1 when it finds the pattern
{
flag=1;
break;
}

}
if(flag==1)
printf("%s\t%s\n",str1,filename1);

}