Wednesday, April 1, 2015

C program to copy one string into another string without using standard function.

Write a ‘C’ program to copy one string into another string without using standard function.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void disp(char *,char *);
void main()
{
char t[20],s[20];
clrscr();
printf("enter the string");
gets(t);
disp(t,s);
getch();
}
void disp(char *t,char *s)
{
while(*t!='\0')
{
*s=*t;
t++;
s++;
}
printf("%s",s);
}

C program to accept m*n matrix from user and display the elements of a given matrix using function.

Write a ‘C’ program to accept m*n matrix from user and display the elements of a given matrix using function.

#include<stdio.h>
#include<conio.h>
void disp(int *,int,int);
void main()
{
int a[3][3],i,j;
clrscr();
printf("enter the matrix element");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
disp(a,3,3);
getch();
}
void disp(int *p,int x,int y)
{
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t",*p);
}
printf("\n");
}
}

C program using command line arguments to search for a word in a file and replace it with the specified word.

Write a ‘C’ program using command line arguments to search for a word in a file and replace it with the specified word.

#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{
FILE *fp,*fq;
char ch;
clrscr();
if(argc!=4)
{
printf("improper no of argument");
exit(0);
}
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("file does not exist");
exit(0);
}
fq=fopen("b.txt","w");
if(fq==NULL)
{
printf("file does not exist");
exit(0);
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
 break;
if(ch==*argv[2])
{
  fputc(*argv[3],fq);
}
else
{
 fputc(ch,fq);
}
}
 fclose(fp);
 fclose(fq);
 fp=fopen("b.txt","r");
 fq=fopen(argv[1],"w");
 while(1)
 {
   ch=fgetc(fp);
   if(ch==EOF)
      break;
   fputc(ch,fq);
 }
 getch();
       }

C program to check whether given string is palindrome or not.

Write a ‘C’ program to check whether given string is palindrome or not.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
int l,f,i;
clrscr();
printf("enter the string");
gets(name);
l=strlen(name);
for(i=0;i<l;i++)
{
if(name[i]!=name[l-1])
{
f=0;
break;
}
else
{
f=1;
l--;
}
}
if(f==1)
{
printf("no is pallindrom");
}
else
{
printf("no is not pallindrom");
}
getch();
}

C program to accept n numbers store all perfect numbers in an array and display this array.

Write a ‘C’ program to accept ‘n’ numbers, store all perfect numbers in an array and display this array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,j,k,n,m,sum,l=0;
clrscr();
printf("enter the range value");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter no");
scanf("%d",&m);
j=1;
sum=0;
while(j<m)
{
k=m%j;
if(k==0)
{
sum=sum+j;
}
j++;
}
if(sum==m)
{
a[l]=m;
l++;
}
}
for(i=0;i<l;i++)
{
printf("%d",a[i]);
}
getch();
}

C program to accept the line from user, delete all the occurrences of word ‘the' from the line and display the result.

Write a ‘C’ program to accept the line from user, delete all the occurrences of word ‘the’ from the line and display the result.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10],res[10];
int l,i,j=0;
clrscr();
printf("enter the string");
gets(name);
l=strlen(name);
for(i=0;i<l;i++)
{
if((name[i]=='t')&&(name[i+1]=='h')&&(name[i+2]=='e'))
{
name[i]=' ';
name[i+1]=' ';
name[i+2]=' ';
}
}
for(i=0;i<l;i++)
{
if(name[i]!=' ')
{
res[j]=name[i];
j++;
}
}
for(i=0;i<j;i++)
{
printf("%c",res[i]);
}
getch();
}

C program to calculate sum of elements of lower triangle of a n*n matrix.

Write a ‘C’ program to calculate sum of elements of lower triangle of a n*n matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,sum=0;
clrscr();
printf("enter the matrix element");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i>j)
{
sum=sum+a[i][j];
}
}
}
printf("%d\n",sum);
getch();
}

Petabit examples

 Petabit examples  A petabit is a unit of digital information or storage, equivalent to 1,000 terabits or 1 million gigabits. To put it into...