PROGRAM TO FIND STRING LENGTH
#include<stdio.h>
main()
{
char str1[10];
char *p;
int i=0;
printf("enter the string to be read \n");
gets(str1);
p=str1;
while((*p)!='\0')
{
i++;
*p++;
}
printf("the length of the string is %d\n",i);
}
PROGRAM TO REVERSE A STRING
#include<stdio.h>
#include<string.h>
void rev(char *s1,char *s2);
main()
{
char s1[10],s2[10];
printf("enter a string \n");
gets(s1);
rev(s1,s2);
printf("reverse string =%s\n",s2);
}
void rev(char *s1,char *s2)
{
int i,j,len;
for(i=0;*(s1+i)!='\0';i++)
;
len=i;
for(j=0;j<len;j++){
*(s2+j)=*(s1+i-1);
--i;
}
*(s2+j)='\0';
}
#include<stdio.h>
main()
{
char str1[10];
char *p;
int i=0;
printf("enter the string to be read \n");
gets(str1);
p=str1;
while((*p)!='\0')
{
i++;
*p++;
}
printf("the length of the string is %d\n",i);
}
PROGRAM TO REVERSE A STRING
#include<stdio.h>
#include<string.h>
void rev(char *s1,char *s2);
main()
{
char s1[10],s2[10];
printf("enter a string \n");
gets(s1);
rev(s1,s2);
printf("reverse string =%s\n",s2);
}
void rev(char *s1,char *s2)
{
int i,j,len;
for(i=0;*(s1+i)!='\0';i++)
;
len=i;
for(j=0;j<len;j++){
*(s2+j)=*(s1+i-1);
--i;
}
*(s2+j)='\0';
}
PROGRAM TO COPY A STRING
#include<stdio.h>
main()
{
char str1[10],str2[10];
char *p,*q;
printf("enter the string str1 to be read \n");
gets(str1);
p=str1;
q=str2;
while((*p)!='\0')
{
*q=*p;
*p++;
*q++;
}
*q='\0';
printf("the copied string in str2 is: %s\n",str2);
}
PROGRAM TO COMPARE TO STRINGS
#include<stdio.h>
#define size 50
int str_comp(char *p,char *q);
main()
{
char s1[size],s2[size];
int flag;
printf("enter the first string to be read\n");
scanf("%s",&s1);
printf("enter the second string to be read\n");
scanf("%s",&s2);
flag=str_comp(s1,s2);
if(flag==0){
printf("not equal\n");
}
else{
printf("equal\n");
}
}
int str_comp(char *p,char *q)
{
int i,flag=1;
for(i=0;*(p+i)!='\0'&&*(q+i)!='\0';i++)
{
if(*(p+i)!=*(q+i)){
flag=0;
break;
}
}
if(*(p+i)=='\0'&& *(q+i)=='\0'&& flag==1)
{
return(1);
}
return(0);
}
PROGRAM TO CONCATENATE TWO STRINGS
#include<stdio.h>
main()
{
char str1[20],str2[20],str3[20];
char *p,*q,*r;
printf("enter the first string to be read\n");
gets(str1);
printf("enter the second string to be read\n");
gets(str2);
p=str1;
q=str2;
r=str3;
while((*p)!='\0')
{
*r=*p;
*p++;
*r++;
}
while((*q)!='\0')
{
*r=*q;
*q++;
*r++;
}
*r='\0';
printf("the concatinated string is :%s\n",str3);
}
PROGRAM TO PERFORM QUEUE OPERATION
#include<stdio.h> //preprossecor directives
# define size 3 // queue size
int ite,front=0,rear=-1,q[size];
void insertq()
{
if(rear==size-1)
{
printf("************************queuoverflow**********************\n");
return ;
}
else
rear=rear+1;
q[rear]=ite;
}
int deleteq()
{
if(front>rear)
return-1;
return q[front++];
}
void display()
{
int i;
if(front>rear)
{
printf("*****************queue is empty****************\n");
return ;
}
printf("contents of the queue\n");
for(i=front;i<=rear;i++)
{
printf("%d\n",q[i]);
}
}
void main()
{
int choice;
for(;;)
{
printf("1:INSERT\n");
printf("2:DELETE\n");
printf("3:DISPLAY\n");
printf("4:EXIT\n");
printf("enter the choice \n");
scanf("%d",& choice);
switch(choice)
{
case 1:
printf("enter the item to be inserted \n");
scanf("%d",&ite);
insertq();
break;
case 2:
ite=deleteq();
if(ite==-1){
printf("***************queue empty**************** \n");}
else
printf("item delete a=%d \n",ite);
break;
case 3 :
display();
break;
default :
exit(0);
}
}
}
No comments:
Post a Comment
we are hear to discuss your queries ,so please feel free to ask any of your queries.