#include<stdio.h>
main()
{
char ch[20],c;
int i,k;
printf("enter the string\n");
gets(ch);
printf("enter a character\n");
c = getchar();
for(k=0;k<=1;k++)
{
for(i=0;ch[i];i++)
{
if(ch[i]==c)
{
strcpy(ch+i,ch+i+1);
}
}
}
for(i=0;ch[i];i++)
{
printf("%c",ch[i]);
}
}
op :
enter the string
jeelebi
enter a character
e
jlbi
#include<stdio.h>
main()
{
char ch[20],temp;
int i,l;
printf("enter the string\n");
gets(ch);
l = strlen(ch);
l = l-1;
for(i=0;i<=(l/2);i++)
{
temp = ch[i];
ch[i] = ch[l-i];
ch[l-i] = temp;
}
for(i=0;i<=l;i++)
{
printf("%c",ch[i]);
}
}
op :
enter the string
praveen
neevarp
#include<stdio.h>
void rev(char *);
main()
{
char ch[20];
printf("enter a string\n");
gets(ch);
rev(ch);
}
void rev(char *ch)
{
if(*ch)
{
rev(ch+1);
printf("%c",*ch);
}
}
----------------------------------------------------------------------------------------------------------------------------------------
op :
enter a string
praveen
neevarp
#include<iostream>
#include<string>
using namespace std;
main()
{
string s;
int i,count=0;
cout<<"enter your string\n";
cin>>s;
for(i=0;s[i];i++)
{
count++;
}
cout<<count<<endl;
}
op :
enter your string
prawin
6
#include<stdio.h>
main()
{
int a[5] = {66,33,77,11,10};
int min,smin,i;
smin = a[0];
for(i=0;i<=4;i++)
{
if(a[i]<min)
{
smin = min;
min = a[i];
}
if(min<a[i]&&a[i]<smin)
{
smin = a[i];
}
}
printf("the first min number z %d\n",min);
printf("the second min number z %d\n",smin);
}
op :
the first min number z 10
the second min number z 11
#include<stdio.h>
main()
{
int a[5] = {55,11,66,77,72};
int max,min,i;
int smax,smin;
max = min = a[0];
smax = smin = a[0];
for(i=0;i<=4;i++)
{
if(a[i]>max)
{
smax = max;
max = a[i];
}
if(max>a[i]&&smax<a[i])
{
smax = a[i];
}
}
printf("the first max element z %d\n",max);
printf("the second max element z %d\n",smax);
}
------------------------------------------------------------------------------------------------------------------------------------------
op:
the first max element z 77
the second max element z 72
#include<stdio.h>
main()
{
int a[5] = {55,99,33,22,10};
int max,min,i;
max = a[0];
min = a[0];
for(i=0;i<=4;i++)
{
if(a[i]>max)
{
max = a[i];
}
if(a[i]<min)
{
min = a[i];
}
}
printf("the max element z %d\n",max);
printf("the min element z %d\n",min);
}
op :
the max element z 99
the min element z 10
condition : all the elements should be sorted order before when u want to find a element through binary search.
#include<stdio.h>
void binary(int *a,int,int,int);
main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int num;
printf("enter the searched num\n");
scanf("%d",&num);
binary(a,0,9,num);
}
void binary(int a[],int start,int end,int num)
{
int mid;
mid = (start+end)/2;
if(a[mid]==num)
{
printf("num z %d\n",a[mid]);
}
else if(a[mid]>num)
{
end = mid-1;
binary(a,start,end,num);
}
else if(a[mid]<num)
{
start = mid+1;
binary(a,start,end,num);
}
}
op :
enter the searched num
3
num z 3
----------------------------------------------------------------------------------------------------------------------
explanation :
1. binary(a,0,9,num);
2.binary (a=0xbfffdd20, start=0, end=9, num=3)
mid = (start+end)/2;
3.step
if(a[mid]==num)
print mid = 4
4.step
else if(a[mid]>num)
print a[mid] = 5
5.step
end = mid-1;
6.step
binary(a,start,end,num);
7.step
binary (a=0xbfffdd20, start=0, end=3, num=3) at binary.c:15
mid = (start+end)/2;
8.step
if(a[mid]==num)
print a[mid] = 2
9.step
else if(a[mid]>num)
print a[mid] = 2
10.step
else if(a[mid]<num)
print a[mid] = 2
11. step
start = mid+1;
12.step
binary(a,start,end,num);
13.step
binary (a=0xbfffdd20, start=2, end=3, num=3) at binary.c:15
mid = (start+end)/2;
14.step
if(a[mid]==num)
15.print a[mid] = 3
step
printf("num z %d\n",a[mid]);
16.print a[mid] = 3
conditions for a linear search :
1.the elements must be a sorted order.
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int n,i;
printf("enter the number which u want to search\n");
scanf("%d",&n);
for(i=0;i<=9;i++)
{
if(a[i] == n)
{
printf("the number is find i.e %d\n",a[i]);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------
op :
enter the number which u want to search
5
the number is find i.e 5
#include<stdio.h>
#define max 4
struct node
{
int data;
struct node *next;
};
void push(struct node **,int);
void pop(struct node **);
void display(struct node *);
main()
{
struct node *head;
head = NULL;
push(&head,10);
push(&head,20);
push(&head,30);
push(&head,40);
push(&head,50);
display(head);
pop(&head);
printf("\nafter poping the element the stack is\n");
display(head);
}
void push(struct node **q,int num)
{
struct node *temp,*r;
static int n=1;
temp = *q;
if(n<=max)
{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = num;
temp->next = *q;
*q = temp;
n++;
}
else
{
printf("stack is full\n");
}
}
void pop(struct node **q)
{
struct node *temp,*old;
temp = *q;
if(temp == NULL)
{
printf("stack is empty\n");
}
else
{
*q = temp->next;
free(temp);
}
}
void display(struct node *q)
{
while(q!=NULL)
{
printf("%d ",q->data);
q = q->next;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
op :
stack is full
40 30 20 10
after poping the element the stack is
30 20 10
#include<stdio.h>
#define max 4
struct node
{
int data;
struct node *next;
};
void push(struct node **,int);
void pop(struct node **);
void display(struct node *);
main()
{
struct node *head;
head = NULL;
push(&head,10);
push(&head,20);
push(&head,30);
push(&head,40);
push(&head,50);
printf("the elements in queue are\n");
display(head);
printf("\n");
pop(&head);
printf("after deleting the elements are\n");
display(head);
}
void push(struct node **q,int num)
{
struct node *temp,*r;
static int n=1;
temp = *q;
if(temp == NULL)
{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = num;
temp->next = NULL;
*q = temp;
}
else
{
if(n<max)
{
while(temp->next!=NULL)
{
temp = temp->next;
n++;
}
r = (struct node*)malloc(sizeof(struct node));
r->data = num;
r->next = NULL;
temp->next = r;
}
else
{
printf("Queue is full\n");
}
}
}
void pop(struct node **q)
{
struct node *temp,*old;
temp = *q;
if(temp == NULL)
{
printf("Queue is empty\n");
}
else
{
*q = temp->next;
free(temp);
}
}
void display(struct node *q)
{
while(q!=NULL)
{
printf("%d ",q->data);
q = q->next;
}
}
-----------------------------------------------------------------------------------------------------------------
op :
Queue is full
the elements in queue are
10 20 30 40
after deleting the elements are
20 30 40
stack : stack means "last in first out".(LIFO)
#include<stdio.h>
#define max 5
struct node
{
int a[max];
int top;
};
void initialize(struct node*);
void push(struct node*,int);
int pop(struct node*);
main()
{
struct node s;
int i;
initialize(&s);
push(&s,20);
push(&s,30);
push(&s,40);
push(&s,50);
push(&s,60);
push(&s,70);
i=pop(&s);
printf("the deleted elent is = %d\n",i);
}
void initialize(struct node *s)
{ s->top = -1;
}
void push(struct node *s,int num)
{
if(s->top == max-1)
{
printf("stack is full\n");
}
else
{
s->top++; s->a[s->top] = num;
}
}
int pop(struct node *s)
{
int d; if(s->top == -1)
{
printf("stack is empty\n");
}
else
{
d = s->a[s->top]; s->top--; return d; }
}
op:stack is fullthe deleted elent is = 60x
Queue : queue means first in first out.(FIFO)
#include<stdio.h>
#define max 3
struct node
{
int a[max];
int first;
int last;
};
void init(struct node*);
void push(struct node*,int);
int pop(struct node*);
main()
{
struct node s;
int i;
init(&s);
push(&s,20);
push(&s,33);
push(&s,12);
push(&s,50);
i = pop(&s);
printf("the deleted number is %d\n",i);
}
void init(struct node *q)
{
q->first = -1;
q->last = -1;
}
void push(struct node *q,int num)
{
if(q->last == max-1)
{
printf("que is full\n");
}
else
{
q->last++;
q->a[q->last] = num;
}
if(q->first == -1)
q->first++;
}
int pop(struct node *q)
{
int n;
if(q->last == -1)
{
printf("que is empty\n");
}
else
{
n = q->a[q->first];
q->first++;
return n;
}
}
op :
que is full
the deleted number is 20
2. number multiply by
2 with out using * operator
#include<stdio.h>
main()
{
int num = 5;
num =
num<<1;
printf("%d",num);
}
Op : 10
Exp : num number
left shift by the 1 position we can get.
1st---------------0101
Bitwise operators :
bitwise operators are used to operate the bit by bit.
1.convert a num
decimal to binary
#include<stdio.h>
main()
{
int i,n;
printf("enter the number\n");
scanf("%d",&n);
for(i=31;i>=0;i--)
{
if(n&1<<i)
printf("1");
else
printf("0");
}
}
Explanation :
This is 31st bit
00000000 00000000 0000000 00000000 ------
> this 0th bit.
In screen the
bits are printing in reverse order. So we go for left
shift.
Steps how to print:
1.cursor goes to 31st position.
2.prform logical and
3.prints
0
Next
00
Next
000
Next
01
like this.so printing on the screen from left to
right.so we go for left shift.
0 comments: