Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

//6. Program to .

#include<stdio.h>
void main(){
int n=0,i,num=0,p=0,in=0,pos=0;

printf("Enter the number of elements = ");


scanf("%d",&n);
int a[n+1];
printf("Enter the value of elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("Enter 1 to insert element at the end.\nEnter 2 to insert element at a


particular position.\n");
printf("Enter 3 to insert element at the beginning.");
scanf("%d",&p);

switch(p)
{
case 1:
printf("Enter the element to insert = ");
scanf("%d",&in);
a[n]=in;
break;
case 2:
printf("Enter the element to insert = ");
scanf("%d",&in);
printf("Enter the position to insert = ");
scanf("%d",&pos);

for(i=n;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos-1]=in;

for(i=0;i<n+1;i++)
printf("%d ",a[i]);
break;
default:
printf("Wrong Choice!!");
}
}
--------------------------------------------------------
//10. Program to perform operations on strings.
#include<stdio.h>
void main() {
int len=0,i,p=0,j=0; char s[100];
printf("Enter the string = ");
gets(s);

for(i=0;s[i]!='\0';i++)
len++;
printf("The length of string is = %d",len);
//------------------------------------------------
char one[]="This is string 1.";
char two[100];
for(i=0;one[i]!='\0';i++)
two[i]=one[i];
printf("\nCopied String 2 = %s",two);
//-------------------------------------------------
char rev[len];
for(i=len-1;i>=0;i--)
rev[i]=s[len-i-1];
rev[len]='\0';
printf("\nReversed String = %s",rev);
//---------------------------------------------
s[len+1]='\0'; s[len]=' ';
char merge[100]; char add[]="is pursuing B.Tech.\n";
for(i=0;s[i]!='\0';i++)
merge[i]=s[i];

for(j=i;add[p]!='\0';j++,p++)
merge[j]=add[p];
merge[j]='\0';
printf("\nMerged String = %s",merge);
//------------------------------------------------
char lc[]="i am a little girl";
char up[100];
for(i=0;lc[i]!='\0';i++)
{
if(lc[i]!=' ')
up[i]=(char)((int)lc[i]-32);
else
up[i]=' ';
}
printf("5. String in Lower Case = %s",lc);
printf("\n String in Upper Case = %s",up);
}
-----------------------------------------------------------------------------------
//8. Program to find an element in an array using linear search.
#include<stdio.h>
void main()
{ int find=0,n=0,i,flag=0;
printf("Enter the number of elements = ");
scanf("%d",&n);
int a[n];
printf("Enter the value of elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element to search = ");
scanf("%d",&find);

for(i=0;a[i]!=0;i++) {
if(a[i]==find)
{
flag++; break;
} }
if(flag==1)
printf("Element %d found at position %d.",find,i+1);
else
printf("Element not found!");
}

-------------------------------------------------------------------------------
//12. Program to find an element in an array using binary search.
#include<stdio.h>
void main()
{ int find=0,n=0,i,mid=0,l=0,r=0,flag=0;
printf("Enter the number of elements = ");
scanf("%d",&n);
int a[n];
printf("Enter the value of elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element to search = ");
scanf("%d",&find);
r=n-1;
while(l<=r)
{
mid=(l+r)/2;
if(a[mid]==find)
{
flag=1; break;
}
else if(a[mid]>find)
r=mid+1;
else
l=mid+1;
}
if(flag==1)
printf("Element %d found at position %d",find,mid+1);
else
printf("Element not found.");
}
-------------------------------------------------------------------

You might also like