You are on page 1of 19

working questions 1.

Write a program that concatenates two strings

#include <stdio.h> /* always */ #include <string.h> /* usually */ int main(int argc, char *argv[]) { char src1[21], src2[21], dest[41]; /* Copy source arguments to arrays, limited to 10 characters max */ strncpy(src1, argv[1], 20); src1[20] = '\0'; strncpy(src2, argv[2], 20); src2[20] = '\0'; /* Concatenate source strings into destination: */ strcpy(dest, src1); strcat(dest, src2); /* Display results: */ printf("%s\n", dest); return 0; }

2.Write a program that reverses an integer. #include<stdio.h> #include<conio.h> #include<stdlib.h> void main(int argc, char **argv) { int num, rev=0,remen; num = atoi(argv[1]); while(num>0) { remen = num % 10; rev = rev*10 + remen; num/=10; } printf("%d " , rev);

3.write a program to compute an expression. #include<stdio.h> #include<conio.h> #include<stdlib.h> void main(int argc, char *argv[]) { int a,b,c,d,t; a = atoi(argv[1]); b = atoi(argv[2]); c = atoi(argv[3]); d = atoi(argv[4]); t = 2*a*5*b+c-6*d*5*c; printf("%d",t); }

4.Write a program that prints out the first 'n' numbers in the Fibonacci sequence. #include<stdio.h> void main(int argc,char *argv[]) { int a=1,b=1,c,ctr=2; printf("%d\n",a); printf("%d",b); while(ctr<atoi(argv[1])) { c=a+b; printf("\n%d",c); a=b; b=c; ctr++; } }

5.Write a program that accepts two numbers as command-line arguments, compute their L.C.M(Least Common Multiple) and print it. #include<stdio.h> #include<conio.h> #include<stdlib.h> void main(int argc, char *argv[]) { int n1,n2,lcm,n; n1 = atoi(argv[1]); n2 = atoi(argv[2]); for(n=1;;n++) { if(n%n1 == 0 && n%n2 == 0) { lcm=n; break; } } printf("%d", lcm); }

6. Write a program that prints the transpose of a matrix #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> void main(int argc,char* argv[]) { FILE *fp; int mat[10][10]; int i,j,n,c=0,c1; fp=fopen(argv[1],"r"); if(fp==NULL) { printf("File doesnot Exist"); exit(0);

} while(!feof(fp)) { fscanf(fp,"%d",&n); c++; } rewind(fp); c1=(int)sqrtl(c); while(!feof(fp)) { for(i=0;i<c1;i++) { for(j=0;j<c1;j++) { fscanf(fp,"%d",&mat[i][j]); } } } for(i=0;i<c1;i++) { for(j=0;j<c1;j++) { printf("%d ",mat[j][i]); } printf("\n"); } fclose(fp); }

7.Write a program to find the sum of series 1+x+x2+x3+x4+....+xn. #include<stdio.h> #include<stdlib.h> #include<math.h> main(int argc,char *argv[]) { int n1,n2,sum=0,i,power; n1=atoi(argv[1]); n2=atoi(argv[2]); power=1; for(i=1;i<=n2;i++) { power=power*n1;

sum=sum+power; } printf("%d",sum+1); }

8.Write a program to print the even numbers. #include<stdio.h> #include<conio.h> #include<stdlib.h> void main(int argc,char* argv[]) { int n,i; n=atoi(argv[1]); for(i=2;i<n;i=i+2) { printf("%d ",i); }printf("\n"); } 9.Write a program to compute the sum of natural numbers #include<stdio.h> void main(int argc,char* argv[]) { long int n; long int i,s=0; n=atoi(argv[1]); for(i=1;i<=n;i++) { s=s+i; } printf("%ld",s); }

10.sum of integer in a file #include<stdio.h> #include<conio.h> #include<stdlib.h> void main(int argc,char* argv[]) { FILE *fp; int s=0,n; fp=fopen(argv[1],"r"); if(fp==NULL) { printf("File doesn't Exist"); exit(0); } while(!feof(fp)) { fscanf(fp,"%d",&n);s+=n; } printf("%d\n",s); fclose(fp); } 11.Write a program that reverses the content of each line in a file #include<stdio.h> #include<conio.h> #include<string.h> void main(int argc,char* argv[]) { FILE *fp; char ar[100]; fp=fopen(argv[1],"r"); while(!feof(fp)) { fgets(ar,1000,fp); strrev(ar); printf("%s\n",ar); } }

12.Write a program that sorts out the words in a file lexicographically

#include<stdio.h>

#include<conio.h> #include<string.h> void main(int argc,char *argv[]) { FILE *fp; char n[40],str[50][150],tmp[40],tm[50][150]; int i=0,j,t,x=0; int k;fp=fopen(argv[1],"r"); while(!feof(fp)) { fscanf(fp,"%s",n); strcpy(str[i],n); i++; } t=i; t=t-1; for(i=0;i<t;i++) for(j=i;j<t;j++) { { if((strcmpi(str[i],str[j]))>0){strcpy(tmp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],tmp); } } } for(i=0;i<t;i++) { strcpy(tm[i],str[i]); } for(k=0;k<t;k++) { printf("%s\n",tm[k]); } fclose(fp); } 13.Write a program that will print all possible combination of letters in a word lexicographically.

#include<stdio.h> #include<stdlib.h> #include<conio.h> # include<string.h> int bsj(int x) {

int i,j,t2,digit,num=0,digit2,t,k=0; static int a[5]; for(i=0;x!=0;i++) { digit=x%10; x=x/10; a[i]=digit; } k=i; for(j=0;j<k;j++) { for(i=0;i<k-1-j;i++) { if(a[i]>a[i+1]) { t2=a[i]; a[i]=a[i+1]; a[i+1]=t2; } } } for(t=0;t<k;t++) { digit2=a[t]; num=num*10+digit2; } return num; } int main(int c,char* argv[]) { char str[7],str1[10]; int res,i,j,len,num[7],z=0,min=0,max=0,d1=0,k; int k1=0,k2=0,t1=0,i1=0,j1=0; strcpy(str,argv[1]); len=strlen(str); for(j1=0;j1<len;j1++) { for(i1=0;i1<len-1-j1;i1++) { if(str[i1]>str[i1+1]) { t1=str[i1]; str[i1]=str[i1+1]; str[i1+1]=t1; } } } for(i=0,j=len;i<len;i++,j--) { min=min*10+(i+1);

max=max*10+j; } for(i=min;i<=max;i++) { res=bsj(i); if(min==res) { z=i; while(z!=0) { d1=z%10; str1[k1++]=str[d1-1]; z=z/10; } str1[k1]='\0'; k1=0; strrev(str1); puts(str1); } } getchar(); return 0; } 14.Write a program to print a field spanning from 5th to 14th bit positions. #include<stdlib.h> #include<stdio.h> #include<conio.h> void main(int argc,char *argv[]) { int hex,b; sscanf(argv[1],"%x",&hex); b=0X3FF0; hex=hex&b; hex=hex>>4; printf("%X",hex); }

15.Write a program to display the given file contents in rank order.

#include<stdio.h> #include<string.h> #include<conio.h> #include<stdlib.h> void main(int argc,char* argv[]) { FILE *fp; char nam[20][20]; char ntemp[20],name[20]; int age[20]; int mark[20],i,j=0; int t; int l=0,ag,mrk; fp=fopen(argv[1],"r"); if(fp==NULL) { printf("File not Exist"); exit(0); } while(!feof(fp)) { fscanf(fp,"%s %d %d\n",name,&ag,&mrk); strcpy(nam[l],name); age[l]=ag;mark[l]=mrk;l++; } for(i=0;i<l;i++) { for(j=i+1;j<l;j++) { if(mark[i]<mark[j]) { t=mark[j]; mark[j]=mark[i]; mark[i]=t; strcpy(ntemp,nam[j]); strcpy(nam[j],nam[i]); strcpy(nam[i],ntemp); t=age[j]; age[j]=age[i]; age[i]=t; } } } for(i=0;i<l;i++) { printf("%s %d %d\n",nam[i],age[i],mark[i]); }

fclose(fp); } 16. Write a program to print the name of all the students who have passed the PRP exam from a unix batch.

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> void main(int argc,char* argv[]) { FILE *fp1,*fp2; char n[20][20],nam[30],d[30][30],dept[30],j; int mark[20],mrk,i=0,temp; fp1=fopen(argv[1],"r"); fp2=fopen(argv[2],"r"); if(fp1==NULL || fp2==NULL) { printf("no file"); exit(0); } while(!feof(fp1)) { fscanf(fp1,"%s %d",nam,&mrk); strcpy(n[i],nam); mark[i]=mrk;i++; } temp=i;i=0; while(!feof(fp2)) { fscanf(fp2,"%s %s",nam,dept); strcpy(n[i],nam); strcpy(d[i],dept); i++; } j=0; for(i=0;i<temp;i++) { if((strcmp(d[i],"unix"))==0) { if(mark[i]>=70) printf("%s\n",n[i]); } } }

17.Write a program to create a binary search tree.

#include<stdio.h> #include<stdlib.h> struct SBst { int iData; struct SBst* psLeft; struct SBst* psRight; }; typedef struct SBst theNode;theNode* theRoot=NULL; void fnCreateTree(FILE*); void fnInorderTraversal(theNode*); int main(int iArgc, char* pcArgv[]) { FILE *fpFilePointer; fpFilePointer=fopen(pcArgv[1],"r"); fnCreateTree(fpFilePointer); printf("Inorder traversal of the Binary search tree is\n"); fnInorderTraversal(theRoot); return (0); } theNode* btnew=NULL; theNode* btmp=NULL; theNode* prev; void fnCreateTree(FILE* fpFilePointer) { int n=0; while(!feof(fpFilePointer)) { btnew=(theNode*)malloc(sizeof(theNode)); fscanf(fpFilePointer,"%d",&n); btnew->psLeft=NULL; btnew->iData=n; btnew->psRight=NULL; if(theRoot==NULL) { theRoot=btnew; } else { btmp=theRoot; prev=NULL; while(btmp!=NULL) {

prev=btmp; if (n<btmp->iData)btmp=btmp->psLeft; else btmp=btmp->psRight; } if(n<prev->iData) prev->psLeft=btnew; else prev->psRight=btnew; } } } void fnInorderTraversal(theNode* psNode) { if(psNode!=NULL) { fnInorderTraversal(psNode->psLeft); printf("%d ",psNode->iData); fnInorderTraversal(psNode->psRight); } }

18.Write a program to create a linked list.

#include<stdio.h> #include<stdlib.h> struct Slist { int iData; struct Slist* psNext; }; typedef struct Slist theNode; theNode* theHead=NULL,*prev=NULL; void fnCreateList(FILE*); void fnDisplay(theNode*); int main(int iArgc, char *pcArgv[]) { FILE* fp; fp=fopen(pcArgv[1],"r"); fnCreateList(fp); printf("Created linked list is \n"); fnDisplay(theHead); return (0);

} void fnCreateList(FILE* fp) { theNode *new1; int m; while(!feof(fp)) { fscanf(fp,"%d",&m); new1=(theNode*)malloc(sizeof(theNode)); new1->iData=m; new1->psNext=NULL; if(theHead==NULL) { theHead=new1; theHead->psNext=NULL; } else { prev=theHead; while(prev->psNext!=NULL) { prev=prev->psNext; } prev->psNext=new1; new1->psNext=NULL; } } } void fnDisplay(theNode* psTraverse) { if(theHead==NULL) printf("Data doesnot exists\n"); else { for(prev=theHead;prev;prev=prev->psNext) printf("%d ",prev->iData); } }

19.

Write a program to create a stack using linked list.

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

struct SStack { int iStackData; struct SStack* psLink; }; typedef struct SStack theStack; /*Top will point to the Top position of the Stack */ theStack* theTop=NULL; /* Function Prototypes */ void fnCreateStack(FILE*); void fnDisplayStack(theStack*); /* Main function */ int main(int iArgc, char *pcArgv[]) { /*fpFile will hold the pointer to the file where the contents to be put on the stack are saved */ FILE* fpFile; fpFile=fopen(pcArgv[1],"r"); fnCreateStack(fpFile); printf("Stack contents are\n"); fnDisplayStack(theTop); return (0); } void fnCreateStack(FILE* fp) { theStack* new1; int m; while(!feof(fp)) { fscanf(fp,"%d",&m); new1=(theStack*)malloc(sizeof(theStack)); new1->iStackData=m; new1->psLink=NULL; if(theTop==NULL) theTop=new1; else { new1->psLink=theTop; theTop=new1; } } }/* fnDisplayStack will display the contents of the stack from top to bottom */ void fnDisplayStack(theStack* Traverse) { theStack* ptr;/*Paste your code to display the stack here */ if(theTop==NULL) printf("Stack is empty\n"); else

{ for(ptr=theTop;ptr;ptr=ptr->psLink) printf("%d ",ptr->iStackData); } } 20.Write a program to create a queue using linked list

#include<stdio.h> #include<stdlib.h> struct SQueue { int iData; struct SQueue* psLink; }; typedef struct SQueue theQueue; /* theRear is a pointer at the rear end of the queue */ theQueue* theRear=NULL; /* theFront is a pointer at the front end of the queue */ theQueue* theFront=NULL; /* Function Prototypes */ void fnCreateQueue(FILE*); void fnDisplay(theQueue*); int main(int iArgc, char* pcArgv[]) { FILE* fpFile; fpFile=fopen(pcArgv[1],"r"); fnCreateQueue(fpFile); printf("Created queue is\n"); fnDisplay(theFront); return (0); } void fnCreateQueue(FILE* fp) { int m; theQueue* new1,*prev; /*Paste your code to create the queue here */ while(!feof(fp)) { fscanf(fp,"%d",&m); new1=(theQueue*)malloc(sizeof(theQueue)); new1->iData=m; new1->psLink=NULL; if(theRear==NULL && theFront==NULL) { theRear=new1;theFront=new1;

theRear->psLink=NULL; } else { prev=theRear; while(prev->psLink!=NULL) { prev=prev->psLink; }prev->psLink=new1; theRear=new1; new1->psLink=NULL; } } } /* fnDisplay will display the content of the queue from front to rear */ void fnDisplay(theQueue* psTraverse) { theQueue* prev; /*Paste your code to display the queue from front here */ if(theFront==NULL) printf("Data doesnot exists\n"); else { for(prev=theFront;prev;prev=prev->psLink) printf("%d ",prev->iData); } }

21.Write a program to create a doubly linked list and display it

#include<stdio.h> #include<stdlib.h> struct SDll { int iData; struct SDll* psLeft; struct SDll* psRight; }; typedef struct SDll theNode; /* theHead will point to the start of the doubly linked list */ theNode* theHead=NULL; /*Function Prototype */ void fnCreateList(FILE*); void fnDisplay(theNode*); int main(int iArgc,char* pcArgv[])

{ FILE* fpFile; fpFile=fopen(pcArgv[1],"r"); fnCreateList(fpFile); printf("Created doubly linked list is\n"); fnDisplay(theHead); return (0); } void fnCreateList(FILE* fp) { int m;theNode* new1,*prev; /* Paste your code to create the doubly linked list here*/ while(!feof(fp)) { fscanf(fp,"%d",&m); new1=(theNode*)malloc(sizeof(theNode)); new1->iData=m; new1->psLeft=NULL; new1->psRight=NULL; if(theHead==NULL) { theHead=new1; theHead->psLeft=NULL; new1->psRight=NULL; } else { prev=theHead; while(prev->psRight!=NULL) prev=prev->psRight; prev->psRight=new1; new1->psLeft=prev; new1->psRight=NULL; } } } /* fnDisplay will display the doubly linked listed from start to end */ void fnDisplay(theNode* psTraverse) { theNode* prev; /*Paste your code to display the doubly linked list here*/ if(theHead==NULL) printf("Data doesnot exists\n"); else { for(prev=theHead;prev;prev=prev->psRight) printf("%d ",prev->iData); } }

You might also like