EmbLogic's Blog

function of strlen

1 #include<stdio.h>
2 #include<stdlib.h>
3 char* ip()
4 {
5          char *s1,ch;
6          int i=0;
7          s1=(char*)malloc(sizeof(char));
8          while(1)
9         {
10                 scanf(“%c”,&ch);
11                 s1=realloc(s1, i+1*sizeof(char));
12                 if(ch==10)
13                 break;
14                 s1[i]=ch;
15                 i++;
16         }
17         printf(“%s\n”,s1);
18         return s1;
19 }
20 int stlen(char*s1)
21 {
22         int i=0;
23         while(1)
24         {
25                 if(s1[i]==0)
26                 break;
27                 i++;
28         }
29         printf(“%d”,i);
30         return i;
31
32 }
33
34 main()
35 {       char * s1;
36         s1=ip();
37         stlen(s1);
38 }

Posted in Uncategorized | Leave a comment

c program to find length of two string using function,pointer and memory allocation technique

#include
#include
char *input();
int length(char *);
int main()
{
char *s1,*s2;
int i;
printf(“Enter string 1:”);
s1=input();
printf(“Enter string 2:”);
s2=input();
i=length(s1);
printf(“Length of string is:%d\n”,i);
}
char *input()
{
char *s,ch;
int i=0;
s=(char *)malloc(sizeof(char));
while(1)
{
scanf(“%c”,&ch);
s=realloc(s,(i+2)*sizeof(char));
if(ch==’.’)
break;
s[i]=ch;
i++;
}
return s;
}
int length(char *s1)
{
int i=0,count=0;
while(*(s1+i)!=”)
{
count++;
i++;
}
return count;
}

Posted in Uncategorized | Leave a comment

c program to compare two string using function,pointer and memory allocation technique

#include
#include
#include
char *input();
int cmp(char * ,char *);
int main()
{
char *a,*b;
int i;
printf(“Enter 1st string:”);
a=input();
printf(“Enter 2nd string:”);
b=input();
i=cmp(a,b);
printf(“Comparision is=%d\n”,i);
}
char *input()
{
char *s1,ch;
int i=0;
s1=(char *)malloc(sizeof(char));
while(1)
{
scanf(“%c”,&ch);
s1=realloc(s1,(i+2)*sizeof(char));
if(ch==10)
break;
s1[i]=ch;
i++;
}
return s1;
}
int cmp(char *a,char *b)
{
int i,j,flag=0;
for(i=0;i<=5;i++)
{
if(*(a+i)!=*(b+i))
{
j=*(a+i)-*(b+i);
return j;

}
}

}

Posted in Data Structures with C | Leave a comment

c program to concatinate two string using function,pointer and memory allocation technique

#include
#include
#include
char *con(char *s1,char *s2);
char *input();
int main()
{
char *s1,*s2;
int i=0;
printf(“Enter string 1:”);
s1=input();
printf(“Enter string 2:”);
s2=input();
// strcat(s1,s2);
s1=con(s1,s2);
printf(“Concatination is: %s\n”,s1);
}
char *input()
{
char *s,ch;
int i=0;
s=(char *)malloc(sizeof(char));
while(1)
{
scanf(“%c”,&ch);
// ch=(char *)malloc(10*sizeof(char));

s=realloc(s,(i+2)*sizeof(char));
if(ch==10)
break;
s[i]=ch;
i++;
}
return s;
}
char *con(char *s1,char *s2)
{
int i=0,j=0;
for(i=0;i<20;i++)
{
if(*(s1+i)=='')
{
*(s1+i)=*(s2+j);
j++;

}

}

return s1;

}

Posted in Data Structures with C | Leave a comment

a program to read all text files in current directory and merge them all into one text file (assignment13 q6) using

#include<stdio.h>
#include<fcntl.h>
#include<dirent.h>
#include<string.h>
int openf(char *);
int main()
{
DIR *i;
struct dirent *k;
int j,l,s,flag=1,fd,fd1;
char *m = “txt.”;
char ch;
fd1 = openf(“new.txt”);
i = opendir(“./test”);
printf(“\n%d\n”,i);

while(( k = readdir(i)) !=  NULL)
{
flag=1;
l=strlen(k->d_name);
for(j = l-1,s=0;j >= l-4,s < 4 ;j–,s++ )
{
if((k->d_name[j])!= m[s])
{
flag=0;
goto q;
}

}
q:         if(flag==1)
{
fd = openf(k->d_name);
while(read(fd, &ch, 1))
write(fd1,&ch,1);
close(fd);
}
}
printf(“\n”);
close(fd1);
}
int openf(char * s)
{
int fd;
fd = open(s,O_RDWR|O_CREAT,0666);
if(fd < 1)
perror(“open error”);
else
printf(“input file open succesfull fd = %d\n”,fd);
return fd;
}

Posted in Uncategorized | Leave a comment

A function foo(int fd, char * buff , int n, int b_size, int skip) that reads to buff from file where file descriptor fd , n blocks of size b_size each , the last argument specifies the number of bits to skip after each block.

#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
int foo(int ,  char *, int , int , int, char *);
int openf( char *);
int main(int argc, char * argv[])
{
int fd,pos,skip,b_size,n;
char ch,*buff;
buff = (char*)malloc(sizeof(char));
printf(“\nBlock size: “);
scanf(“%d”,&b_size);
printf(“\nNumber of Blocks: “);
scanf(“%d”,&n);
printf(“\nBytes to be skiped after each block: “);
scanf(“%d”,&skip);
fd = openf(argv[1]);
foo(fd,buff,b_size,n,skip,argv[1]);
close(fd);
}
int foo(int fd,  char *buff, int b_size, int n, int skip ,char * f)
{
char ch;
int i=0,j=0,k=0,m=0;
printf(“\nPHASE ONE DONE!!\n”);
while(1)
{
i=0;
for(j=0;;j++)
{    if(j>=n)goto i;
for(k=0; k < b_size; k++)
{
if (!(read(fd, &ch, 1)) || ch==0)
i:    goto o;
*(buff+k) = ch;
buff = realloc(buff,k+1);
}
for(m=0;m < skip;m++)
read(fd, &ch, 1);

printf(“\n%s**\n”,buff);
free(buff);
buff = (char*)malloc(sizeof(char));
}

}
o:printf(“\n%d blocks of size %d skipping %d bits after each read  are read form file %s\n”,n,b_size,skip,f);

}
int openf(char * s)
{
int fd;
fd = open(s,O_RDWR|O_CREAT,0666);
if(fd < 1)
perror(“open error”);
else
printf(“input file open succesfull fd = %d\n”,fd);
return fd;
}

Posted in Uncategorized | Leave a comment

program to make a 4gb file with free space. file with holes.

#include<stdio.h>
#include<fcntl.h>
int openf(char *);
int main(int argc, char * argv[])
{
int fd,fd1, pos,i=1000000;
char ch=”;
fd = openf(argv[1]);
pos = lseek(fd,i * 4096, SEEK_SET);
write(fd, &ch,1);
close(fd);
}
int openf(char * s)
{
int fd;
fd = open(s,O_RDWR|O_CREAT,0666);
if(fd < 1)
perror(“open error”);
else
printf(“input file open succesfull fd = %d\n”,fd);
return fd;
}

Posted in Uncategorized | Leave a comment

program to read text from a file and store it in reverse order

#include<stdio.h>
#include<fcntl.h>
int openf(char *);
int main(int argc, char * argv[])
{
int fd,fd1, pos,i=3;
char ch;
fd = openf(argv[1]);
fd1 = openf(argv[2]);
pos = lseek(fd, 0, SEEK_END);
printf(“\n%d\n”,pos);
for(i=2; i < pos+1;i++)
{
lseek(fd, pos-i, SEEK_SET);
read(fd, &ch, 1);
write(fd1, &ch, 1);
printf(“ch = %c\n”,ch);
}

close(fd);
close(fd1);
}
int openf(char * s)
{
int fd;
fd = open(s,O_RDWR|O_CREAT,0666);
if(fd < 1)
perror(“open error”);
else
printf(“input file open succesfull fd = %d\n”,fd);
return fd;
}

Posted in Uncategorized | Leave a comment

using selection sort, making array elements in accending order which are given by user……….

1#include<stdio.h>
2 #include<stdlib.h>
3
4 int main()
5 {
6     int *a,i=0,j=0,k,l,m,b,least;
7         a=(int *)malloc(sizeof(int));
8
9     while(1)
10     {
11
12         printf(“\n\nenter the elements in an array, remember at ’0′ insertion of elements in    array will be terminated     : \n”);
13
14      while(1)
15       {
16         scanf(“%d”,a+j);
17         if(a[j]==0)
18         break;
19         a=realloc(a,i+2*sizeof(int));
20         i=i+sizeof(int);
21         j++;
22       }
23
24
25         for(k=0;k<=j;k++)
26         {
27             for(l=k;l<=j;l++)
28             {
29                 least=a[k];
30
31                 if(least>a[l])
32                 {
33                   b=a[l];
34                   m=least;
35                   a[k]=b;
36                   a[l]=m;
37
38                  }
39             }
40         }
41
42
43               printf(“\n\nthe array in accending order is : \n”);
44
45                 for(k=0;k<=j;k++)
46                     printf(“%d\n”,a[k]);
47
48   }
49   return 0;
50 }

Posted in Data Structures with C | Leave a comment

4-bit compression

1
2 RCS file: mdc.c,v
3 Working file: mdc.c
4 head: 1.33
5 branch:
6 locks: strict
7 access list:
8 symbolic names:
9 keyword substitution: kv
10 total revisions: 33;    selected revisions: 33
11 description:
12 Creating the rcs file
13 first run
14 —————————-
15 revision 1.33
16 date: 2014/07/12 18:21:44;  author: root;  state: Exp;  lines: +19 -5
17 Done with 4-bit compression.
18 Successfull compression and decompression of a text file.
19 —————————-
20 revision 1.32
21 date: 2014/07/12 12:03:06;  author: root;  state: Exp;  lines: +23 -9
22 *** empty log message ***
23 —————————-
24 revision 1.31
25 date: 2014/07/12 09:31:24;  author: root;  state: Exp;  lines: +11 -9
26 Correcting the last character problem.
27 —————————-
28 revision 1.30
29 date: 2014/07/12 00:38:44;  author: root;  state: Exp;  lines: +1 -81
30 ./
31 —————————-
32 revision 1.29
33 date: 2014/07/11 11:26:07;  author: root;  state: Exp;  lines: +3 -8
34 *** empty log message ***
“log_file” 175L, 6840C                                                                                                              1,0-1         Top

Posted in Project 2: Multiple Data Compression and Encryption | Leave a comment

RCS file: fops.h,v
Working file: fops.h
head: 1.1
branch:
locks: strict
root: 1.1
access list:
symbolic names:
keyword substitution: kv
total revisions: 1;    selected revisions: 1
description:
struct file_operations fops={
2 open:device_open,
3 release:device_release,
4 write:write,
5 read:read,
6 llseek:llseek
7 };
—————————-
revision 1.1    locked by: root;
date: 2014/07/12 12:01:39;  author: root;  state: Exp;
Initial revision
=============================================================================

Posted in Uncategorized | Leave a comment

Program For selection Short

#include<stdio.h>
int main()
{
int i,j,n,small,k;
printf(“Enter the Element of Array\n”);
scanf(“%d”,&n);
int a[n];
printf(“Enter the element\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
small=i;
for(j=i;j<n;j++)
{
if(a[j]<a[small])
small=j;
}
k=a[small];
a[small]=a[i];
a[i]=k;
}
for(i=0;i<n;i++)
{
printf(“%d\n”,a[i]);
}
}

Posted in Uncategorized | Leave a comment

A program For Binary Search

#include<stdio.h>
int main()
{
int i,j,n,temp,search,low,high,mid,c;
printf(“Enter the element of array \n “);
scanf(“%d”,&n);
int a[n];
printf(“Enter the element\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(“Array After Shorting\n”);
for(i=0;i<n;i++)
{
printf(“a[%d]=%d\n”,i,a[i]);
}
printf(“Enter the Number To be Search :\n”);
scanf(“%d”,&search);
low=0;
high=n-1;
while(1)
{
mid=(high+low)/2;
if(search==a[mid])
{
printf(“%d is found %dth location \n”,search,mid);
break;
}
else if(search>a[mid])
{
low=mid+1;
high=n-1;
}
else if(search<a[mid])
{
low=0;
high=mid-1;
}
else if(low>high)
{
printf(“%d is not found\n”,search);
break;
}
}
}

Posted in Uncategorized | Leave a comment

Completed the mdc for all bits, from 1 to 8

After working on mdc for 2 weeks, now I have completed it. Using general function for compression and decompression. After n number of iterations, the code length has been reduced to 136 written lines. I am trying to reduce it further more. The whole program is modular, and divided into sub-programs and codes.

I have created a global function for compression, which is capable of doing the compression and encryption from 1 to 7. It can also encrypt the 8 bit data files.
I have implemented a new filter technique for decompressing, which is quite reliable, and is usable for any bit decompression.

I tried to reduce the code as much as I could, till now. The code-length of compression function is now 22 lines., and that of decompression is 29 lines.

I am not posting the code here, but if anyone wants it for study or modification. He/she may contact me on my email id.

utkarshdwivedi3394@gmail.com

 

Posted in Project 2: Multiple Data Compression and Encryption | Leave a comment

c program for selection sort.

#include
int main()
{
int a[100],i,j,small,n,k;
printf(“Enter size of array:”);
scanf(“%d”,&n);
printf(“Enter element:\n”);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);

}
printf("Element and their position are:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<n;i++)
{
small=i;
for(j=i;j<n;j++)
{
if(a[j]<a[small])
small=j;
}
k=a[small];
a[small]=a[i];
a[i]=k;
}
printf("Arrar after selection sort:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}

Posted in Data Structures with C | Leave a comment