Вы находитесь на странице: 1из 4

int main(){

int i,j,temp1,temp2;
int arr[8]={5,3,0,2,12,1,33,2};
int *ptr;
for(i=0;i<7;i++)
{
for(j=0;j<7-i;j++)
{
if(*(arr+j)>*(arr+j+1))
{
ptr=arr+j;
temp1=*ptr++;
temp2=*ptr;
*ptr--=temp1;
*ptr=temp2;
}
}
}

// Sorting an array using pointers

for(i=0;i<8;i++)
printf(" %d",arr[i]);
}
Output: 0 1 2 2 3 5 12 33
/*PROGRAM TO FIND MAXIMU
M NUMBER IN ARRAY USING POINTER*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,max;
int *p;
clrscr();
printf(Enter the size of array: );
scanf(%d,&n);
printf(Enter %d elements in the array:\n,n);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
printf(Elements in the array are:\n);
for(i=0;i<n;i++)
printf(%5d,a[i]);
p=&a[0];
max=a[0];
for(i=0;i<n;i++)
{
if(max<=*p)
max=*p;
p++;
}
printf(\nMaximum element in the array is: %d,max);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,sum=0;

int *p;
clrscr();
printf(Enter the size of array: );
scanf(%d,&n);
printf(Enter %d elements in the array:\n,n);
ay using pointers
for(i=0;i<n;i++)
scanf(%d,&a[i]);
p=&a[0];
printf(Entered elements are:\n);
for(i=0;i<n;i++)
{
printf(%5d,*p);
sum=sum+(*p);
p++;
}
printf(\nSum of elements of array is: %d,sum);
getch();
}

// Add n elements in arr

#include<stdio.h>
#include<conio.h>
void main()
{
int n,a;
int *rev,*rem,*temp;
clrscr();
printf(Enter any number: );
scanf(%d,&n);
a=n;
temp=&n;
*rev=0;
while(*temp>0)
{
*rem=*temp%10;
*temp=*temp/10;
*rev=(*rev)*10+*rem;
}
printf(Reverse of %d is %d,a,*rev);
getch();
}
#include<stdio.h>
int main(){
int i=0,j=0;
char *str1,*str2,*str3;
puts("Enter first string");
// Concatenation of 2 strings us
ing pointers
gets(str1);
puts("Enter second string");
gets(str2);
printf("Before concatenation the strings are\n");
puts(str1);
puts(str2);
while(*str1){
str3[i++]=*str1++;
}
while(*str2){
str3[i++]=*str2++;
}

str3[i]='\0';
printf("After concatenation the strings are\n");
puts(str3);
return 0;
}
/*PROGRAM TO DETERMINE L
ENGTH OF STRING USING POINTERS*/
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10],*p;
int i=0;
clrscr();
printf(Enter any string: );
gets(a);
p=a;
while(*p!=\0)
{
i++;
p++;
}
printf(Length of given string is: %d,i);
getch();
}
/*PROGRAM TO SWAP TWO NU
MBRES USING POINTERS*/
#include<stdio.h>
#include<conio.h>
void swap(int *, int *);
void main()
{
int a,b;
clrscr();
printf(Enter any two numbers: );
scanf(%d%d,&a,&b);
printf(Before swapping numbers are: a=%d, b=%d\n,a,b);
swap(&a,&b);
printf(After swapping numbers are: a=%d, b=%d,a,b);
getch();
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
#include<stdio.h>
void copystr(char*,char*);
// Copying a string to another u
sing pointers
void main()
{
char*str1="I am Aditya Krishnakumar";
char str2[30];
clrscr();
copystr(str2,str1);

printf("\n %s",str2);
getch();
}
void copystr(char *dest,char *src)
{
while(*src!= \0 )
*dest++=*src++;
*dest= \0 ;
return;
}
#include<stdio.h>
#include<string.h>
int search(char*, char*);
int main()
{
int loc;
char *source = "maharashtra";
// Finding substring in a progra
m using pointers
char *target = "sht";
loc = search(source, target);
if (loc == -1)
printf("\nNot found");
else
printf("\nFound at location %d", loc + 1);
return (0);
}
int search(char *src, char *str)
{
int i, j, firstOcc;
i = 0, j = 0;
while (*(src+i) != \0 )
{
while (*(src+i) != *(str+0) && *(src+i) != \0 )
i++;
if (*(src+i) == \0 )
return (-1);
firstOcc = i;
while (*(src+i) == *(str+j) && *(src+i) != \0 && *(str+j) != \0 )
{
i++;
j++;
}
if (*(str+j) == \0 )
return (firstOcc);
if (*(src+i) == \0 )
return (-1);
i = firstOcc + 1;
j = 0;
}
}

Вам также может понравиться