You are on page 1of 13

POINTERS

1. Program to count frequency of every character present in a line of text.

#include<stdio.h>
#include<string.h>

void frequencyofuniquecharacter(char *s)


{
int i,j,count=0,n;
n=strlen(s);
printf(" frequency count character in string:\n");

for(i=0;i<n;i++)

count=1;

if(s[i])

for(j=i+1;j<n;j++)

if(s[i]==s[j])

count++;

s[j]='\0';

printf(" '%c' = %d \n",s[i],count);

int main()

char s[1000];

printf("String to be taken: ");

gets(s);

frequencyofuniquecharacter(s);
return 0;

}
2. Program to swap two numbers.

#include <stdio.h>

void swap(int *a,int *b)

int t;

t = *a;

*a = *b;

*b = t;

int main()

int x,y;

printf("Enter x: ");

scanf("%d",&x);

printf("Enter y: ");

scanf("%d",&y);

printf("Before Swapping the numbers: x is: %d, y is: %d\n",x,y);

swap(&x,&y);

printf("After Swapping the numbers: x is: %d, y is: %d\n",x,y);

return 0;

}
3. Program to find area and circumference of a circle.

#include <stdio.h>

void Area_Perimeter(float *r,float *area,float *peri)

*area=3.1415*(*r)*(*r);

*peri=2*3.1415*(*r);

int main(){

float radius,area,peri;

printf("\nEnter the radius of the circle:");

scanf("%f",&radius);

Area_Perimeter(&radius,&area,&peri);

printf("\nThe Area of the circle is :%.3f",area);

printf("\nThe Perimeter of the circle is :%.3f",peri);

return 0;

}
4. Write functions for the following string operations.

a) Concatenation. b) Comparison. c) Length d) Copy e) Reverse.

#include <stdio.h>

#include <stdlib.h>

int lengh(char*str)

int count = 0;

while (*str != '\0') {

count++;

str++;

return count;

void concat(char *m, char *n)

while(*m != '\0')

m++;

while(*n != '\0')

*m=*n;

m++;

n++;

*m = '\0';

void copy(char *m, char *n)


{

while(*n)

*m = *n;

n++;

m++;

*m = '\0';

void reverse(char *str)

char reverse[100];

int length = lengh(str);

for (int i = 0; i < length; i++)

reverse[i]=*(str+length-1-i);

for (int i = 0; i < length; i++)

*(str+i) = reverse[i];

void compare(char *m, char *n)

while(*m == *n)

if ( *m == '\0' || *n == '\0' )

break;
m++;

n++;

if( *m == '\0' && *n == '\0' )

printf("\nBoth Strings Are Equal");

else

printf("\nBoth Strings Are Not Equal");

int main(){

char m[50],n[50];

int a;

printf("\nEnter value of a:");

scanf("%d",&a);

if(a==1)

printf("==LENGTH FUNCTION==\n");

printf("Enter the string:\n");

scanf("%s",m);

printf("The Length of the string is %d\n",lengh(m));

if(a==2)

printf("==Reverse function==\n");

printf("\nEnter the string:");

scanf("%s",m);

reverse(m);

printf("\nThe reverse of string will be:");


puts(m);

if(a==3)

printf("==Concatenation function==\n");

printf("\nEnter the string 1:");

scanf("%s",m);

printf("\nEnter the string 2:");

scanf("%s",n);

concat(m,n);

printf("\nString after concatenation:");

puts(m);

if(a==4)

printf("==Copy function==\n");

printf("\nEnter the string 1:");

scanf("%s",m);

printf("\nEnter the string 2:");

scanf("%s",n);

copy(m,n);

printf("\nafter copying:");

puts(n);

if(a==5)

printf("==Compare function==\n");

printf("\nEnter the string 1:");

scanf("%s",m);

printf("\nEnter the string 2:");

scanf("%s",n);
compare(m,n);

else if (a>=6)

printf("==Wrong command==");

return 0;

}
5. Write a program to display the greatest of N numbers –use malloc() function.

#include<stdio.h>

#include<stdlib.h>

int main(){

int n;

int *p;

printf("Enter number of elements:");

scanf("%d",&n);

p = (int*)malloc(n * sizeof(int));

printf("==MEMORY ALLOCATED==\n");

for (int i = 0; i < n; i++)

printf("Enter the Element %d\n",i+1);

scanf("%d",p+i);

for(int i = 1; i < n; ++i)

if(*p < *(p + i))

*p = *(p + i);

printf("The Greatest Element of the given list of number is %d ",*p);

return 0;

}
6. Write a program to arrange N names in alphabetical order using dynamic memory allocation.

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

void alphaorder(int n,char *x[])

int i,j;

char t[20];

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++){

if(strcmp(x[i],x[j])>0)

strcpy(t,x[j]);

strcpy(x[j],x[i]);

strcpy(x[i],t);

int main()

char *x[20];

int i,n=0;

void alphaorder(int n,char *x[]);

printf("Enter no.of names : ");

scanf("%d",&n);

printf("\n");

for(i=0;i<n;i++)

printf("Enter the names %d : ",i+1);

x[i]=(char *)malloc(20*sizeof(char));

scanf("%s",x[i]);
}

alphaorder(n,x);

printf("\nlist in alphabetical order : \n");

for(i=0;i<n;i++)

printf("%s\n",x[i]);

return 0;

You might also like