pet solutions (complete set)
TRANSCRIPT
-
8/2/2019 Pet Solutions (Complete Set)
1/28
.Program 1: Write a program that concatenates two strings#include
#include
#include
int main(int argc,char *argv[])
{ if(argc0)
{
r=num%10;
num=num/10;
s=s*10+r;
}
printf("%d",s);
return 0;
}
Program 3: Write a program that computes an expression
#include
#include
int main(int argc,char *argv[])
{
int a,b,c,d,f;
a=atoi(argv[1]);
b=atoi(argv[2]);
c=atoi(argv[3]);
d=atoi(argv[4]);
-
8/2/2019 Pet Solutions (Complete Set)
2/28
f=2*a*5*b+c-6*d*5*c ;
printf("%d",f);
return 0;
}
Program 4: Write a program that prints out the first 'n' numbers in
the Fibonacci sequence.
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int prev=1,next=1,limit,i=0;
limit=atoi(argv[1]);
if(limit==0)
{
printf("\n Should be greater than 0");
return 1;
}
while(im2)
{
num=m1;
-
8/2/2019 Pet Solutions (Complete Set)
3/28
den=m2;
}
else
{
num=m2;
den=m1;
}
rem=num%den;
while(rem!=0)
{
num=den;
den=rem;
rem=num%den;
}
return den;
}
Program 6: Write a program that prints the transpose of a matrix
#include
#include
int main(int argc,char *argv[])
{
char ch,c[6];
int i=0,j=0,k=0,m,n,a[10][10];
FILE *f;
f=fopen(argv[1],"r");
while((ch=fgetc(f))!=EOF)
{
if(ch=='\n'||ch==' ')
{
c[k]='\0';
a[i][j]=atoi(c);
k=0;j++;
if(ch=='\n')
{
j=0;
i++;
continue;
}
}
else
{
c[k]=ch;
k++;
}
}
fclose(f);
c[k]='\0';
a[i][j]=atoi(c);
for(m=0;m
-
8/2/2019 Pet Solutions (Complete Set)
4/28
}
printf("\n");
}
return 0;
}
Program 7: Write a program to find the sum of series1+x+x2+x3+x4+....+xn.
#include
#include
#include
void main(int argc,char *argv[])
{
int i,x,n,sum=0;
x=atoi(argv[1]);
n=atoi(argv[2]);
for(i=0;i
-
8/2/2019 Pet Solutions (Complete Set)
5/28
num++;
}
printf("%d",sum);
return 0;
}
Program 10: Write a Program that prints out the sum of the integersin the file.
#include
#include
int main(int argc,char *argv[])
{
FILE *fp;
int sum=0,num=0;
fp=fopen(argv[1],"r");
while((fscanf(fp,"%d",&num))!=EOF)
{
sum+=num;
}
printf("%d",sum);
return 0;
}
Program 11: Write a Program that prints out the sum of the integersin the file.#include
#include
#include
int main(int argc,char *argv[])
{
FILE *fp; char ch,a[1000];
int i=0,j=0;
fp=fopen(argv[1],"r");
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')
{
a[i]='\0';
i=strlen(a);
for(j=i-1;j>=0;j--)
printf("%c",a[j]);
printf("\n");
i=0;
} else
{
a[i]=ch;
i++;
}
}
a[i]='\0';
i=strlen(a);
-
8/2/2019 Pet Solutions (Complete Set)
6/28
for(j=i-1;j>=0;j--)
{
printf("%c",a[j]);
}
return 0;
}
Program 12: Write a program that sorts out the words in a filelexicographically.
#include
#include
#include
int main(int argc,char *argv[])
{
int i,j,len=0;
char *temp,t[100];
char *a[100];
FILE *fp=fopen(argv[1],"r");
while(fscanf(fp,"%s",t)!=EOF)
{
a[len++]=strdup(t);
}
for(i=0;i
-
8/2/2019 Pet Solutions (Complete Set)
7/28
void sort(char ch[20]);
char b[10];
int l;
int main(int argc,char *argv[])
{
sort(argv[1]);
}
void sort(char ch[20])
{
int i,j,len;
int n;
char temp;
len=strlen(ch);
for(i=0;i
-
8/2/2019 Pet Solutions (Complete Set)
8/28
}
}
Program 14: Write a program to print a field spanning from 5th to 14th
bit positions.
#include#include
#include
#include
int main(int argc,char *argv[])
{
char inp[20],bin[200]=" ",resbin[200]=" " ;
char *p;
int i,j=0,dec=0,k;
if(argv[1][0]=='0'&&argv[1][1]=='x')
strcpy(inp,argv[1]+2);
else
strcpy(inp,argv[1]);
for(p=inp;*p!='\0';++p){
switch(*p)
{
case'0':
strcat(bin,"0000");
break;
case'1':
strcat(bin,"0001");
break;
case'2':
strcat(bin,"0010");
break;
case'3':
strcat(bin,"0011"); break;
case'4':
strcat(bin,"0100");
break;
case'5':
strcat(bin,"0101");
break;
case'6':
strcat(bin,"0110");
break;
case'7':
strcat(bin,"0111");
break; case'8':
strcat(bin,"1000");
break;
case'9':
strcat(bin,"1001");
break;
case'A':
strcat(bin,"1010");
break;
-
8/2/2019 Pet Solutions (Complete Set)
9/28
case'B':
strcat(bin,"1011");
break;
case'C':
strcat(bin,"1100");
break;
case'D':
strcat(bin,"1101");
break;
case'E':
strcat(bin,"1110");
break;
case'F':
strcat(bin,"1111");
break;
}
}
k=strlen(bin);
for(i=strlen(bin)-5;i>=strlen(bin)-14;--i)
resbin[j++]=bin[i];
resbin[j]='\0';strrev(resbin);
for(j=0,i=strlen(resbin)-1;i>=0;--i,++j)
if(resbin[i]=='1')
dec=dec+pow((double)2,(double)j);
printf("\n%X",dec);
return(0);
}
Program 15: Write a program to display the given file contents inrank order.
#include
#include#include
struct stud
{
char ch[20];
int roll;
int marks;
};
staticint tot;
int main(int argc,char*argv[])
{
FILE *fp;
struct stud a;
struct stud s[100]; char temp[20];
int temp2=0;
int temp1=0;
int i=0,j=0;
fp=fopen(argv[1],"r");
while(fscanf(fp,"%s%d%d",&a.ch,&a.roll,&a.marks)!=EOF)
{
strcpy(s[i].ch,a.ch);
s[i].roll=a.roll;
-
8/2/2019 Pet Solutions (Complete Set)
10/28
s[i].marks=a.marks;
i++;
}
tot=i;
for(i=0;i
-
8/2/2019 Pet Solutions (Complete Set)
11/28
{
if(marks[m]>=70&&strcmp(sub[m],"unix")==0&&strcmp(nm[m],nm1[m])==0)
{
printf("%s\n",nm[m]);
}
}
return 0;
}
Program 17: Write a program to create a binary search tree.
#include
#include
#include
#include
#include
struct Node
{
int data;
struct Node *left;
struct Node *right;
};
typedefstruct Node tree;
tree *theRoot=NULL;
//theRoot=(tree *)malloc(sizeof(tree));
void inOrder(tree *);
void createTree(FILE *);
int main(int argc,char* argv[])
{
FILE *fp;
fp=fopen(argv[1],"r");
createTree(fp);
fclose(fp);printf("Inorder traversal of the Binary search tree is\n");
inOrder(theRoot);
return 0;
}
void inOrder(tree *p)
{
if(p!=NULL)
{
inOrder(p->left);
printf("%d ",p->data);
inOrder(p->right);
}
return;
}
void createTree(FILE *fp)
{
tree *newNode,*temp,*temp2;
int num=0,flag=0;
while(fscanf(fp,"%d",&num)!=EOF)
{
newNode=(tree*)malloc(sizeof(tree));
-
8/2/2019 Pet Solutions (Complete Set)
12/28
newNode->data=num;
newNode->left=NULL;
newNode->right=NULL;
temp=theRoot;
temp2=temp;
if(theRoot==NULL)
{
theRoot=newNode;
continue;
}
while(temp)
{ temp2=temp;
//printf("%d %d\n",temp->data,newNode->data);
if(temp->data>newNode->data)
{
temp=temp->left;
//printf("left\n");
flag=0;
}else
{
temp=temp->right;
//printf("right\n");
flag=1;
}
}
if(flag==0)
{temp2->left=newNode;
//printf("added on left \n");
}else
{ temp2->right=newNode;
//printf("added on right \n");
}
}
return ;
}
Program 18: Write a program to create a linked list.
#include
#include
#include#include
#include
struct Node
{
int info;
struct Node *next;
-
8/2/2019 Pet Solutions (Complete Set)
13/28
};
typedefstruct Node ll;
ll *theHead=NULL;
//theRoot=(tree *)malloc(sizeof(tree));
void createLL(FILE *);
int main(int argc,char* argv[])
{
FILE *fp;
ll *p=NULL;
fp=fopen(argv[1],"r");
createLL(fp);
fclose(fp);
printf("Created linked list is\n");
p=theHead;
while(p!=NULL)
{
printf("%d ",p->info);
p=p->next;
}return 0;
}
void createLL(FILE *fp)
{
ll *nLink,*temp;
int num=0;
while(fscanf(fp,"%d",&num)!=EOF)
{
nLink=(ll*)malloc(sizeof(ll));
nLink->info=num;
nLink->next=NULL;
temp=theHead;
if(theHead==NULL)
{
theHead=nLink;
continue;
}
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nLink;
}
return ;}
Program 19: Write a program to create a stack using linked list.
#include
#include
-
8/2/2019 Pet Solutions (Complete Set)
14/28
#include
#include
#include
struct Node
{
int info;
struct Node *next;
};
typedefstruct Node Stack;
Stack *theTop=NULL;
void createStack(FILE *);
int main(int argc,char* argv[])
{
FILE *fp;
Stack *p=NULL;
fp=fopen(argv[1],"r");
createStack(fp);
fclose(fp);printf("Stack contents are\n");
p=theTop;
while(p!=NULL)
{
printf("%d ",p->info);
p=p->next;
}
return 0;
}
void createStack(FILE *fp)
{
Stack *nLink;
int num=0;
while(fscanf(fp,"%d",&num)!=EOF)
{
nLink=(Stack*)malloc(sizeof(Stack));
nLink->info=num;
nLink->next=NULL;
if(theTop==NULL)
{
theTop=nLink;
continue;
}
nLink->next=theTop;
theTop=nLink;
}
return ;
}
-
8/2/2019 Pet Solutions (Complete Set)
15/28
Program 20: Write Write a program to create a queue using linkedlist
#include
#include
#include#include
#include
struct Node
{
int info;
struct Node *next;
};
typedefstruct Node Queue;
Queue *theFront=NULL;
Queue *theRear=NULL;
void createQueue(FILE *);int main(int argc,char* argv[])
{
FILE *fp;
Queue *p=NULL;
fp=fopen(argv[1],"r");
createQueue(fp);
fclose(fp);
printf("Created queue is\n");
p=theFront;
while(p!=NULL)
{
printf("%d ",p->info);
p=p->next;
}
return 0;
}
void createQueue(FILE *fp)
{
Queue *nLink;
int num=0;
while(fscanf(fp,"%d",&num)!=EOF)
{
nLink=(Queue*)malloc(sizeof(Queue));
nLink->info=num;
nLink->next=NULL;
if(theFront==NULL){
theFront=nLink;
theRear=nLink;
continue;
}
theRear->next=nLink;
theRear=nLink;
}
-
8/2/2019 Pet Solutions (Complete Set)
16/28
return ;
}
Program 21: Write a program to create a doubly linked list anddisplay it.
#include
#include
#include
#include
#include
struct Node
{
int info;
struct Node *prev;
struct Node *next;
};
typedefstruct Node Dll;
Dll *theHead=NULL;
void createDll(FILE *);
int main(int argc,char* argv[])
{
FILE *fp;
Dll *p=NULL;
fp=fopen(argv[1],"r");
createDll(fp);
fclose(fp);
printf("Created doubly linked list is\n");
p=theHead;while(p!=NULL)
{
printf("%d ",p->info);
p=p->next;
}
return 0;
}
void createDll(FILE *fp)
{
Dll *nLink,*temp,*temp2=NULL;
int num=0;
while(fscanf(fp,"%d",&num)!=EOF)
{nLink=(Dll*)malloc(sizeof(Dll));
nLink->info=num;
nLink->next=NULL;
nLink->prev=NULL;
temp=theHead;
if(theHead==NULL)
{
theHead=nLink;
-
8/2/2019 Pet Solutions (Complete Set)
17/28
continue;
}
while(temp->next!=NULL)
{
temp2=temp;
temp=temp->next;
}
temp->next=nLink;
nLink->prev=temp2;
}
return ;
}
Program 22: Write a program to convert ip address to hexadecimal.
#include#include#include#include
int main(int argc, char* argv[]){
char outbuf[3]={0};char a[4];int x=0,i=0,j=0,len=0;//argv[1]="255.0.0.0";//printf("%s\n",argv[1]);len=strlen(argv[1]);while(i
-
8/2/2019 Pet Solutions (Complete Set)
18/28
#include#include
int main(int argc,char *argv[]){
FILE *fp; char a[100];
int i=0,flag=0,len =0;fp=fopen(argv[1],"r");
i=0; while(fscanf(fp,"%s",&a)!=EOF)
{
len=strlen(a);while(i(int)a[i+1]){
flag=1;
}i++;
}
if(flag==0){
printf("%s\n",a);}flag=0;i=0;
}
return 0;}
Program 24: Write a program to find missing number in fibbonacciseries.
#include#include
int main(int argc, char* argv[]){
int prev=1,next=1,num=0;FILE *fp;fp=fopen(argv[1],"r");while(fscanf(fp,"%d",&num)!=EOF){
if(num!=prev){
printf("%d",num);return 0;
}next=prev+next;prev=next-prev;
}return 0;
-
8/2/2019 Pet Solutions (Complete Set)
19/28
}
Program 25: Write a program to delete from a linked list.
#include#include#include#include#include
struct Node{
int info;struct Node *next;
};typedefstruct Node ll;ll *theHead=NULL;
void createLL(FILE *);void deleteLL(int,int );
int main(int argc,char* argv[]){
FILE *fp;ll *p=NULL;fp=fopen(argv[1],"r");createLL(fp);
fclose(fp);printf("Created linked list is\n");p=theHead;
while(p!=NULL){
printf("%d ",p->info);p=p->next;
}deleteLL(atoi(argv[2]) ,atoi(argv[3]));printf("\n\n\n");p=theHead;while(p!=NULL)
{printf("%d ",p->info);p=p->next;
}return 0;
}void createLL(FILE *fp){
ll *nLink,*temp;int num=0;while(fscanf(fp,"%d",&num)!=EOF){
nLink=(ll*)malloc(sizeof(ll));nLink->info=num;nLink->next=NULL;
-
8/2/2019 Pet Solutions (Complete Set)
20/28
temp=theHead;if(theHead==NULL){
theHead=nLink;continue;
}
while(temp->next!=NULL){
temp=temp->next;}temp->next=nLink;
}
return ;}
void deleteLL(int iData,int oData){
ll *temp;temp=theHead;while(temp->next!=NULL){
if(temp->info==iData){
temp->info=oData;return ;
}temp=temp->next;
}return ;
}
Program 26: Write a program to insert into a linked list.
#include#include#include#include#include#include
struct Node{
int info;struct Node *next;
};typedefstruct Node ll;ll *theHead=NULL;void BinsertLL(int,int);void AinsertLL(int,int);
-
8/2/2019 Pet Solutions (Complete Set)
21/28
void createLL(FILE *);
int main(int argc,char* argv[]){
FILE *fp;ll *p=NULL;fp=fopen(argv[1],"r");
createLL(fp);
fclose(fp);printf("Created linked list is\n");p=theHead;while(p!=NULL)
{printf("%d ",p->info);p=p->next;
}printf("\n\n\n");
insertLL(atoi(argv[2]),atoi(argv[3]));printf("\n\n\n");p=theHead;while(p!=NULL)
{printf("%d ",p->info);p=p->next;
}return 0;
}void createLL(FILE *fp){
ll *nLink,*temp;int num=0;while(fscanf(fp,"%d",&num)!=EOF){
nLink=(ll*)malloc(sizeof(ll));nLink->info=num;nLink->next=NULL;
temp=theHead;if(theHead==NULL){
theHead=nLink;continue;
}
while(temp->next!=NULL){
temp=temp->next;
}temp->next=nLink;}
return ;}
void BinsertLL(int iData,int oData)
-
8/2/2019 Pet Solutions (Complete Set)
22/28
{ll *temp,*temp2,*nLink;nLink=(ll*)malloc(sizeof(ll));nLink->info=oData;nLink->next=NULL;temp=theHead;temp2=temp;
while(temp->info!=NULL){
if(temp->info==iData){
nLink->next=temp;temp2->next=nLink;return ;
}temp2=temp;temp=temp->next;
}return ;
}
void AinsertLL(int iData,int oData){
ll *temp,*nLink;nLink=(ll*)malloc(sizeof(ll));nLink->info=oData;nLink->next=NULL;temp=theHead;
while(temp->next!=NULL){
if(temp->info==iData){
nLink->next=temp->next;temp->next=nLink;return ;
}temp=temp->next;
}return ;
}
------------------------Program 21: Write a program to find missing number in ArithmaticProgression.
#include
#include
int main(int argc, char* argv[])
-
8/2/2019 Pet Solutions (Complete Set)
23/28
{
int a=4,d=4,num=0,b[100],i=0;
FILE *fp;
fp=fopen(argv[1],"r");
while(fscanf(fp,"%d",&num)!=EOF)
{
b[i]=num;
i++;
}
a=0;
d=b[1]-b[0];
fseek(fp,0,0);
while(fscanf(fp,"%d",&num)!=EOF)
{
a=a+d;
if(a!=num)
{
printf("%d",a);
return 0;
}
}
fclose(fp);
return 0;
}
Program : Write a program to overwrite the existing node in a singlylinked list.
#include
#include
struct SSll
{
int iData;
struct SSll* psNext;
};
typedefstruct SSll theNode;
/* theHead will point to the start of the singly linked list */
theNode* theHead=NULL;
/*Function Prototype */
void fnCreateList(FILE*);
void fnDisplay(theNode*);
void fnOverwrite(int,int);
int main(int iArgc,char* pcArgv[])
{
FILE* fpFile;
fpFile=fopen(pcArgv[1],"r");
fnCreateList(fpFile);
printf("Singly linked list before overwrite the node\n");
fnDisplay(theHead);
-
8/2/2019 Pet Solutions (Complete Set)
24/28
fnOverwrite(atoi(pcArgv[2]),atoi(pcArgv[3]));
printf("\nSingly linked list after overwrite the node\n");
fnDisplay(theHead);
return (0);
}
/* fnDisplay will display the singly linked listed from start to end */
void fnDisplay(theNode* psTraverse)
{
while(psTraverse!=NULL)
{
printf("%d ",psTraverse->iData);
psTraverse=psTraverse->psNext;
}
}
/* fnCreateList will read the data from the file and create a singly linked
list */
void fnCreateList(FILE* fpFile)
{
theNode *nLink,*temp;
int num=0;while(fscanf(fpFile,"%d",&num)!=EOF)
{
nLink=(theNode*)malloc(sizeof(theNode));
nLink->iData=num;
nLink->psNext=NULL;
temp=theHead;
if(theHead==NULL)
{
theHead=nLink;
continue;
}
while(temp->psNext!=NULL)
{
temp=temp->psNext;
}
temp->psNext=nLink;
}
fclose(fpFile);
return ;
}
/* fnOverwrite will overwrite the node based on the given node in the singly
linked list */
void fnOverwrite(int iFindData, int iOverwriteData)
{/*Paste your code here to overwrite the existing node in singly linked
list */
theNode *temp=NULL;
temp=theHead;
while(temp->iData!=iFindData)
{
temp=temp->psNext;
}
-
8/2/2019 Pet Solutions (Complete Set)
25/28
temp->iData=iOverwriteData;
return ;
}
STACK
#include
#include
#include
struct SStack
{
int iStackData;
struct SStack* psLink;
};
typedefstruct SStack theStack;
/*Top will point to the Top position of the Stack */
theStack* theTop=NULL;
/* Function Prototypes */
void fnCreateStack(FILE*);
void fnDisplayStack(void);
void fnPop(int);
int fnCountStack(void);
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 before popping are\n");
fnDisplayStack();
fnPop(atoi(pcArgv[2]));
printf("\nStack contents after popping are\n");
fnDisplayStack();
printf("\nCount of stack after popping is %d",fnCountStack());
return (0);
}
/* fnDisplayStack will display the contents of the stack from top to bottom */
void fnDisplayStack(){
theStack* Traverse;
Traverse=theTop;
while(Traverse)
{
printf("%d ",Traverse->iStackData);
Traverse=Traverse->psLink;
-
8/2/2019 Pet Solutions (Complete Set)
26/28
}
}
/*fnCreate Stack will read the contents from file, create the stack
dynamically with the contents from the stack*/
void fnCreateStack(FILE* fp)
{
theStack *nLink;
int num=0;
while(fscanf(fp,"%d",&num)!=EOF)
{
nLink=(theStack*)malloc(sizeof(theStack));
nLink->iStackData=num;
nLink->psLink=NULL;
if(theTop==NULL)
{
theTop=nLink;
continue;}
nLink->psLink=theTop;
theTop=nLink;
}
return ;
}
/*fnPop will remove the node from the list till it reaches to the searching
number*/
void fnPop(int ifindNo)
{
while(theTop->iStackData!=ifindNo)
{
theTop=theTop->psLink;
}
theTop=theTop->psLink;
}
/* fnCountStack will return the number of elements in the stack */
int fnCountStack()
{
int i=0;
while(theTop!=NULL){
theTop=theTop->psLink;
i++;
}
//printf("%d",i);
return i;
}
-
8/2/2019 Pet Solutions (Complete Set)
27/28
ANAgram
#include#include
#include
#include
char *sort(char ch[20]);
int main(int argc,char *argv[])
{
char a[100],b[100];
strcpy(a,sort(argv[1]));
strcpy(b,sort(argv[2]));
if(strcmp(a,b)==0)
{printf("YES");
}
else
{
printf("NO");
}
return 0;
}
char* sort(char ch[20])
{
int i,j,len; int n;
char temp;
len=strlen(ch);
for(i=0;i
-
8/2/2019 Pet Solutions (Complete Set)
28/28
#include
int main(int argc, char* argv[])
{
int num=0,sum=0,i=0,a[100],j=0,len=0;
FILE *fp;
fp=fopen(argv[1],"r");
while(fscanf(fp,"%d",&num)!=EOF)
{
sum=sum+num;
//printf("%d\n",num);
a[i]=num;
i++;
}
len=i;
for(i=0;i