0% found this document useful (0 votes)
78 views52 pages

Assignment-Ii Computer Programming

The document contains C code solutions to 5 programming assignments involving arrays, pointers, functions and sorting algorithms: 1. A function to calculate statistics of a 2D array using pointers. 2. Analyzing I/O buffering and streaming with character/string functions. 3. Functions to manipulate a string based on if it is a palindrome. 4. A function to call other functions and return values using pointers. 5. Implementing insertion sort and selection sort on dynamically allocated arrays.

Uploaded by

Neeraj Yadav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views52 pages

Assignment-Ii Computer Programming

The document contains C code solutions to 5 programming assignments involving arrays, pointers, functions and sorting algorithms: 1. A function to calculate statistics of a 2D array using pointers. 2. Analyzing I/O buffering and streaming with character/string functions. 3. Functions to manipulate a string based on if it is a palindrome. 4. A function to call other functions and return values using pointers. 5. Implementing insertion sort and selection sort on dynamically allocated arrays.

Uploaded by

Neeraj Yadav
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

ASSIGNMENT-II COMPUTER PROGRAMMING

CODES

Submitted by: Name= Himanshu Kumar Pandey Stream= MCA Reg. No.= 2012CA93 Batch= 2012-2015

1. WAP using a single function that will return the following from a 2-D array of integers: A. Mean of the elements in the array B. Standard Deviation of the elements in the array C. Maximum element in the array D. Minimum element in the array E. Address of the maximum element in the array. Use Pointer Arithmetic, Pointer Notation and appropriate prototyping for the function. /*coded by: Himanshu Kumar Pandey reg. no.= 2012ca93 date: 07/11/2012 */ #include<stdio.h> int arr(int*,int); int main() { int max,array[3][3],i,j,*ptr; printf("Enter 9 elements in array.\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&array[i][j]); max=arr(array,3);

for(i=0;i<3;i++) for(j=0;j<3;j++) { if(array[i][j]==max) ptr=&array[i][j]; } printf("Address of Maximum Element %d is %u\n",max,ptr); return 0; } int arr(int *ar,int a) { int i,j,*p; p=ar; int c=0,min,max; float mean,sum; for(i=0;i<a;i++) for(j=0;j<a;j++) { sum+=*ar; c++; ar++; } //printf("sum=%f",sum);

mean=sum/c; printf("Mean of Array is: %f\n",mean); ar=p; printf("Standard deviation of array: "); for(i=0;i<a;i++) for(j=0;j<a;j++) { sum+=(*ar-mean)*(*ar-mean); ar++; } printf("%f\n",sum/c); printf("Finding Minimum element in array:\n"); ar=p; min=*ar; for(i=0;i<a;i++) for(j=0;j<a;j++) { if(*ar<min) min=*ar; ar++; } printf("Minimum element is: %d\n",min); printf("Finding Maximum element in array:\n");

ar=p; max=*ar; for(i=0;i<a;i++) for(j=0;j<a;j++) { if(*ar>max) max=*ar; ar++; } printf("Maximum element is: %d\n",max); return max; }

2. Find out the difference between I/O Buffering & Streaming. Analyze how the following character & string i/o functions behave with the character left in buffer: A. getchar & putchar B. fgets & fputs

C. gets & puts D. scanf & printf Demonstrate with a small C program for each of the above if the pair of functions working together more than once in a program can show any abrupt behavior. /*coded by: Himanshu Kumar Pandey Reg. No.: 2012ca93 date: 08/11/12*/ #include<stdio.h> #include<string.h> void getchar_putchar(); void fgets_fputs(); void gets_puts(); void scanf_printf(); int main() { char ch; do { printf("\nChecking getchar() putchar().\n"); getchar_putchar(); printf("\nwant to Check More Cases(y/n):"); scanf("%c",&ch);

getchar(); }while(ch=='y'|| ch=='Y'); do { printf("\nChecking fgets() fputs() FUNCTION.\n"); fgets_fputs(); printf("\nwant to Check More Cases(y/n):"); scanf("%c",&ch); getchar(); }while(ch=='y'|| ch=='Y'); do { printf("\nChecking gets() puts().\n"); gets_puts(); printf("\nwant to Check More Cases(y/n):"); scanf("%c",&ch); getchar(); }while(ch=='y'|| ch=='Y'); do { printf("\nChecking scanf() printf().\n"); scanf_printf(); printf("\nwant to Check More Cases(y/n):");

scanf("%c",&ch); getchar(); }while(ch=='y'|| ch=='Y'); return 0; } void getchar_putchar() { static char str1[20],str2[20]; int i=0; printf("\nEnter Your String:"); printf("(PRESS CTRD+D TO STOP)\n"); while((str1[i]=getchar())!=EOF) i++; str1[i]='\0'; printf("\nEnter Second String:"); printf("(PRESS CTRD+D TO STOP)\n"); i=0; while((str2[i]=getchar())!=EOF) i++; str2[i]='\0'; printf("\n"); for(i=0;i<20;i++) putchar(str1[i]);

printf("\n"); for(i=0;i<20;i++) putchar(str2[i]); } void fgets_fputs() { static char str1[20],str2[20]; printf("Enter the first string:\n"); fgets(str1,20,stdin); printf("enter the second string:\n"); fgets(str2,20,stdin); fputs(str1,stdout); fputs(str2,stdout); } void gets_puts() { static char str1[20],str2[20]; printf("Enter the first string:\n"); gets(str1); printf("Enter the second string:\n"); gets(str2); puts(str1); puts(str2);

} void scanf_printf() { static char str1[20],str2[20]; printf("Enter the first string:\n"); scanf("%s",str1); getchar(); printf("\nEnter the second string:\n"); scanf("%s",str2); getchar(); printf("%s",str1); printf("%s",str2); }

3. WAP using Functions, Pointer Arithmetic and Pointer Notation. Specification of the program is as follows: main() will call a function store() to read a string character by character in an array declared inside it, and

return the address of the first element of the array. Then, main() will call a function ptest() to test, if the string is a palindrome or not and, will return 1 if the string is a palindrome, 0 otherwise to main(). If the string is a palindrome then the main() will read a character from the user and search that character in the string by calling a function csearch(). If the given character is present then, main() will read another character (replacing character) from the user and call a function replace() to replace each occurrence of searched character by the replacing character. If the string is not a palindrome then, main() will call a function reverse() to will reverse the string. Finally main() will call a function print() to display the string. In all you have to use six functions in addition to main(). There will be no array declaration in main(). Use appropriate prototyping for functions. /*coded by: Himanshu Kumar Pandey Reg. no.: 2012ca93 date: 08/11/12 */ #include<stdio.h> #include<string.h> int length; char* store();

int ptest(char*,int); int csearch(char*,char); char* replace(char*,char,char); void reverse(char*); void print(char*); int main() { char* strptr,repch,repbych; strptr=store(); int test=ptest(strptr,length); if(test==1) { printf("\nString is palindrome\n"); printf("Enter any character to be searched:"); scanf("%c",&repch); int search=csearch(strptr,repch); if(search==1) { printf("%c Is present.",repch); printf("\nEnter the character to be replaced:"); getchar(); scanf("%c",&repbych); strptr=replace(strptr,repch,repbych);

} else printf("\n%c Is not present.",repch); } else { printf("\nString is not palindrome.\n"); reverse(strptr); } print(strptr); return 0; } char* store() { static char str[20]; int i=0; printf("Enter the string(PRESS CTRL+D TO STOP):\n"); while((str[i]=getchar())!=EOF) i++; length=i; str[i]='\0'; return str; }

int ptest(char* strptr,int length) { int i,j,flag=1; for(i=0,j=length-1;i<length/2;i++,j--) { if(*(strptr+i) != *(strptr+j)) { flag=0; break; } } if(flag) return 1; else return 0; } int csearch(char* strptr,char ch) { int i; for(i=0; *(strptr+i) ;i++) { if(*(strptr+i)!=ch) {

continue; } else { return 1; } } } char* replace(char* strptr,char repch,char repbych) { int i; for(i=0; *(strptr+i) ;i++) { if(*(strptr+i)==repch) { *(strptr+i)=repbych; } } return strptr; } void reverse(char* strptr) { int i,j;

char temp; for(i=0,j=length-1;i<j;i++,j--) { temp= *(strptr+i); *(strptr+i) = *(strptr+j); *(strptr+j) = temp; } } void print(char* strptr) { printf("FINAL STRING:"); printf("%s\n",strptr); } 4. WAP using Pointer Notation to solve a problem with following specifications: Let multicall() be a function having two parameters, first one a pointer to a function, second a pointer to an integer. main() will read an integer and call multicall() by passing the address of the integer and the address of one of the four functions: digitcount(), paltest(), reverse() and primetest(). multicall() will call one of the four functions and return appropriate value to main(). Descriptions of the functions are as follows: digitcount() : Return number of digits in an integer.

paltest() : Return 0 if integer is a palindrome, 1 otherwise. reverse() : Return reverse of a given integer. primetest() : Return 1 if integer is a prime, 0 otherwise. main() will print the following after reading an integer: A. number of digits in the number B. if the number is a palindrome or not C. reverse of the number D. if the given number is a prime or not. /*coded by: Himanshu Kumar Pandey reg. no.: 2012ca93 date=08/11/12 */ #include<stdio.h> int digitcount(int); int paltest(int); int reverse(int); int primetest(int); int multicall(int*,int (*fp)(int)); int main() { int n,dc,pt,re,prt; printf("Enter your Number:");

scanf("%d",&n); dc=multicall(&n,digitcount); printf("You have eneterd %d digit Number.\n",dc); pt=multicall(&n,paltest); if(pt==0) printf("Number is Palindrome.\n"); else printf("Number is not Palindrome.\n"); re=multicall(&n,reverse); printf("Reverse of %d is %3d\n",n,re); prt=multicall(&n,primetest); if(prt==1) printf("Entered number is Prime Number.\n"); else printf("Entered number is not Prime Number.\n"); return 0; } int multicall(int *a,int (*fp)(int)) { int x; x=(*fp)(*a); return x; }

int digitcount(int a) { int c=0; while(a>0) { a=a/10; c++; } return c; } int paltest(int a) { int p; p=reverse(a); if(a==p) return 0; else return 1; } int reverse(int a) { int rem,rev=0; while(a>0)

{ rem=a%10; rev=rev*10+rem; a=a/10; } return rev; } int primetest(int a) { int i,p=1; for(i=2;i<a;i++) { if(a%i==0) { p=0; break; } } return p; } 5. Implement following sorting on arrays of integers allocated dynamically: A. Insertion Sort /*coded by: Himanshu Kumar Pandey

Reg. No.: 2012ca93 date: 08/11/12 */ #include<stdlib.h> #include<stdio.h> #define SIZE 10 void insort(int*); int main() { int *ptr_arr,i; ptr_arr=(int *)malloc(sizeof(int)*SIZE); printf("Enter the array elements to be sort: \n"); for(i=0;i<SIZE;i++) scanf("%d",(ptr_arr+i)); insort(ptr_arr); printf("Sorted array is:\n"); for(i=0;i<SIZE;i++) printf("%d ",*(ptr_arr+i)); printf("\n"); } void insort(int *ptr_arr) { int i,j,item;

for(j=1;j<SIZE;j++) { item=*(ptr_arr+j); for(i=j-1;i>=0 && item< *(ptr_arr+i);i--) { *(ptr_arr+(i+1))= *(ptr_arr+i); } *(ptr_arr+(i+1))=item; } } B. Selection Sort /*coded by: Himanshu Kumar Pandey Reg. no.: 2012ca93 date: 08/11/12 */ #include<stdio.h> #include<stdlib.h> #define SIZE 10 void selsort(int *); int main() { int *ptr_arr; int temp,i,j;

ptr_arr=(int *)malloc(sizeof(int)*SIZE); printf("Enter the array to be sorted:\n"); for(i=0;i<SIZE;i++) scanf("%d",(ptr_arr+i)); selsort(ptr_arr); printf("Sorted array is:\n"); for(i=0;i<SIZE;i++) printf("%d ",*(ptr_arr+i)); printf("\n"); return 0; } void selsort(int *ptr_arr) { int i,j,temp; for(i=0;i<SIZE-1;i++) { for(j=i+1;j<SIZE;j++) { if(*(ptr_arr+i)>*(ptr_arr+j)) { temp = *(ptr_arr+i); *(ptr_arr+i) = *(ptr_arr+j); *(ptr_arr+j)= temp;

} } } } C. Merge Sort /*coded by: Himanshu Kumar Pandey Reg. No.: 2012ca93 date: 08/11/12 */ #include<stdio.h> #include<stdlib.h> #define SIZE 10 int mergesort(int*,int,int); int merge(int*,int,int,int); int main() { int *ptr_arr,i; ptr_arr=(int*)malloc(sizeof(int)*SIZE); printf("Enter the elements:\n"); for(i=0; i<SIZE; i++) scanf("%d",(ptr_arr+i)); mergesort(ptr_arr,0,SIZE-1); // sort the array printf("Sorted array:\n"); // print sorted array

for(i=0; i<SIZE; i++) printf("%d ",*(ptr_arr+i)); return 0; } int mergesort(int* ptr_arr,int low,int high) { int mid; if(low<high) { mid=(low+high)/2; // Divide and Conquer mergesort(ptr_arr,low,mid); mergesort(ptr_arr,mid+1,high); // Combine merge(ptr_arr,low,mid,high); } return 0; } int merge(int*ptr_arr,int l,int m,int h) { int *arr1,*arr2; arr1=(int*)malloc(sizeof(int)*(SIZE/2)); arr2=(int*)malloc(sizeof(int)*(SIZE/2)); int n1,n2,i,j,k; n1=m-l+1;

n2=h-m; for(i=0; i<n1; i++) { *(arr1+i)= *(ptr_arr+l+i); } for(j=0; j<n2; j++) { *(arr2+j)= *(ptr_arr+m+j+1); } *(arr1+i)=9999; // To mark the end of each temporary array *(arr2+j)=9999; i=0; j=0; for(k=l; k<=h; k++) //process of combining two sorted arrays { if(*(arr1+i)<= *(arr2+j)) *(ptr_arr+k)= *(arr1+(i++)); else *(ptr_arr+k)= *(arr2+(j++)); } return 0; } D. Quick Sort

/*coded by: Himanshu Kumar Pandey reg. No.: 2012ca93 Date: 08/11/12 */ #include<stdio.h> #include<stdlib.h> #define SIZE 10 void quicksort(int*,int,int); int partition(int*,int,int) ; int main( ) { int *arr; int i ; arr=(int*)malloc(sizeof(int)*SIZE); printf("Enter the elements:\n"); for(i=0;i<SIZE;i++) scanf("%d",(arr+i)); quicksort(arr,0,9); printf ("\nArray after sorting:\n") ; for (i=0;i<=9;i++) printf ("%d\t",arr[i]); printf("\n"); }

void quicksort(int a[],int lower,int upper) { int i ; if ( upper > lower ) { i = partition ( a, lower, upper ) ; quicksort( a, lower, i - 1 ) ; quicksort( a, i + 1, upper ) ; } } int partition(int a[],int lower,int upper) { int i,p,q,t; p = lower + 1 ; q = upper ; i = a[lower] ; while ( q >= p ) { while ( a[p] < i ) p++ ; while ( a[q] > i ) q-- ; if ( q > p )

{ t = a[p] ; a[p] = a[q] ; a[q] = t ; } } t = a[lower] ; a[lower] = a[q] ; a[q] = t ; return q ; } E. Radix Sort /*coded by: Himanshu Kumar Pandey reg no.: 2012ca93 date: 08/11/12 */ #include <stdio.h> #include<stdlib.h> #define SIZE 10 void radixsort(int *); int main() { int *arr;

int i, n; arr=(int*)malloc(sizeof(int)*SIZE); printf("Enter The Elements : \n"); for (i = 0; i < SIZE; i++) scanf("%d",(arr+i)); radixsort(arr); printf ( "\nArray after sorting:\n") ; for ( i = 0 ; i < SIZE ; i++ ) printf ( "%d\t", *(arr+i) ) ; printf("\n"); return 0; } void radixsort(int *arr) { int i,exp=1; int max_value= *(arr+0); int *b; b=(int*)malloc(sizeof(int)*SIZE); for (i = 0; i < SIZE; i++) { if (*(arr+i) > max_value) max_value= *(arr+i); }

while (max_value / exp > 0) { int *bucket; bucket=(int*)malloc(sizeof(int)*(SIZE*2)); for (i = 0; i < SIZE; i++) { *(bucket+ (*(arr+i)/ exp % 10))= *(bucket+ (*(arr+i)/ exp % 10))+1; } for (i = 1; i <SIZE*2; i++) { *(bucket+i) += *(bucket+i-1); } for (i =SIZE-1; i >= 0; i--) { *(b+(--*(bucket+(*(arr+i) / exp % 10)))) = *(arr+i); } for (i = 0; i < SIZE; i++) { *(arr+i) = *(b+i); } exp *= 10; } }

6. Implement a menu-driven program on Stack of integers and strings using array for following operations: A. Initialise: declare a dynamic stack B. Check_IsEmpty: if stack is empty C. Check_IsFull: if stack is full D. Pop: pop out topmost element and print it E. Push: push element into stack F. Read_top: Just read the top element dont pop it G. Count_element: Find no of Stack element currently H. Display_Stack: Print the current elements in stack from top to bottom I. Exit: exit from the program /*coded by: Himanshu Kumar Pandey reg. no.: 2012ca93 date: 08/11/12 */ #include <stdio.h> #include <stdlib.h> struct stack //Create Structure Of Stack. { int num; char str[25]; }*p; //Pointer Variable To Store Address Of Stack // Created Dynamically Using Malloc.

void Initialise(); //Function Prototype. void Check_IsEmpty(); void Check_IsFull(); void Pop(); void Push(); void Read_top(); void Count_element(); void Display_Stack(); void Exit(); int top=-1; //Global Stack Pointer And Size. int size; int main() { char choice; do { printf("\nA. Initialise: declare a dynamic stack"); printf("\nB. Check_IsEmpty: if stack is empty"); printf("\nC. Check_IsFull: if stack is full"); printf("\nD. Pop: pop out topmost element and print it"); printf("\nE. Push: push element into stack"); printf("\nF. Read_top: Just read the top element dont pop it"); printf("\nG. Count_element: Find no of Stack element currently");

printf("\nH. Display_Stack: Print the current elements in stack from top to b ottom"); 16,2 9% printf("\nI. Exit: exit from the program"); printf("\nPlease!Enter Your Choice:"); choice=getchar(); switch (choice) { case 'A':Initialise(); break; //Function Calling According To Choice. case 'B':Check_IsEmpty(); break; case 'C':Check_IsFull(); break; case 'D':Pop(); break; case 'E':Push(); break; case 'F':Read_top(); break; case 'G':Count_element(); break; case 'H':Display_Stack(); break;

case 'I':Exit(); break; default:("\nWrong Choice You Have Entered"); } getchar(); } while (choice!='I'); } void Initialise() { printf("\nEnter Size Of Stack:"); scanf("%d",&size); p=(struct stack *)malloc(size*sizeof(struct stack)); //Dynamic Memory Allocation. 52,2 32% if (p==NULL) { printf("\nStack Not Created\n"); return; } else { printf("\nStack Successfully Created\n"); } }

void Check_IsEmpty() //Function Defination Of Check_IsEmpity. { if (top==-1) printf("\nStack Is Empity"); else printf("\nStack Is Not Empity"); } void Check_IsFull() //Function Defination Of Check_IsFull. { if (top==size-1) printf("\nStack Is Full"); else printf("\nStack Is Not Full"); } void Pop() //Function Defination Of Pop. { if (top==-1) printf("\nStack Is Empty"); else { printf("\n%d And %s Is Deleted From Stack",(p+top)->num,(p+top)->str ); 87,2 55% top--;

} } void Push() //Function Defination Of Push. { if (top==size-1) { printf("\nStack Is Full"); } else { top++; printf("\nEnter Number:"); scanf("%d",&(p+top)->num); getchar(); printf("\nEnter String:"); fgets((p+top)->str,25,stdin); } } void Read_top() //Function Defination Of Read_top. { if (top==-1) { printf("\nStack Is Empty");

} else { printf("\nTop Element\n"); printf("\nNumber=%d\nString=%s",(p+top)->num,(p+top)->str); } } void Count_element() //Function Defination Of Count_element. { int count=0; while (count<=top) count++; printf("\nTotal No Of Element=%d",count); } int i; if (top==-1) { printf("\nStack Is Empty"); } else { printf("\nlist Of Element Stored In Stack\n"); printf("\nNumber String");

for(i=top;i>=0;i--) "stack.c" 17{L, 3933C printf("\n%d\t\t%s",(p+i)->num,(p+i)->str); } } } void Exit() //Function Defination Of Exit. { return 0; } PART B : PROBLEMS ON STRUCTURES & UNIONS 7. WAP using a single function that will return the following from a 2-D array of integers: A. Mean of the elements in the array B. Standard Deviation of the elements in the array C. Maximum element in the array D. Minimum element in the array E. Address of the maximum element in the array. Use Pointer Arithmetic, Pointer Notation and appropriate prototyping for the function. /*Coded by: Himanshu Kumar Pandey Reg. No.: 2012ca93 date: 09/11/12

*/ #include<stdio.h> #include<stdlib.h> #include<math.h> //use gcc -lm to compile void calculate(int *,int,double *,double *,int *,int *,int **);//Function Prototype int main() //start of main function { struct temp //Structure Declarations { int n,*arr,i,max,min,*m_add; double mean,sd; }var; printf("\n***Array Calculation***\n"); //Input printf("\nEnter the number of elements to be stored in Array: "); scanf("%d",&var.n); var.arr=(int*)malloc(var.n*sizeof(int)); //Allocate memory dynamically printf("\nEnter the Array Elements\n"); for(var.i=0;var.i<var.n;var.i++) //Input Array Elements scanf("%d",(var.arr+var.i)); calculate(var.arr,var.n,&var.mean,&var.sd,&var.max,&var.min,&var.m_add); printf("Mean is: %lf\n",var.mean); //Display printf("Standard Deviation is: %lf\n",var.sd); printf("Maximum Number is: %d\n",var.max);

printf("Minimum Number is: %d\n",var.min); printf("Maximum Number Address is: %x\n",var.m_add); free(var.arr); return 0; }//end of main function //Function to do the calculations void calculate(int *arr,int n,double *mean,double *sd,int *max,int *min,int **max_add) { int i; //Variable Declaration double sum,sumation,var; for(i=0;i<n;i++) //Calculate Mean sum+=*(arr+i); *mean=sum/n; for(i=0;i<n;i++) //Calculate Standard Deviation sumation+=(*(arr+i)-*mean)*(*(arr+i)-*mean); var=sumation/n; *sd=sqrt(var); //calculate maximum & minimum & return Maximum Number Address *max=*(arr+0); *min=*(arr+0); for(i=0;i<n;i++) {

if(*max<*(arr+i)) { *max=*(arr+i); *max_add=(arr+i); } if(*min>*(arr+i)) *min=*(arr+i); } }//end of function 8. WAP using Functions, Pointer Arithmetic and Pointer Notation. Specification of the program is as follows: main() will call a function store() to read a string character by character in an array declared inside it, and return the address of the first element of the array. Then, main() will call a function ptest() to test ,if the string is a palindrome or not and, will return 1 if the string is a palindrome, 0 otherwise to main().If the string is a palindrome then the main() will read a character from the user and search that character in the string by calling a function csearch(). If the given character is present then, main() will read another character (replacing character) from the user and call a function replace() to replace each occurrence of searched character by the replacing character. If the string is not a palindrome then, main() will call a

function reverse() to will reverse the string. Finally main() will call a function print() to display the string. In all you have to use six functions in addition to main(). There will be no array declaration in main(). Use appropriate prototyping for functions. /*Coded by: Himanshu Kumar Pandey reg. No.: 2012ca93 date: 09/11/12 */ #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 50 char *store(); //Function Prototype int ptest(char *); int esearch(char *,char); void replace(char *,char,char); char *reverse(char*); int main() //Main Function { typedef struct temp //Structure Declaration { char *ptr;

char ch,rh,blk; }charTemp; charTemp charStr,*T; T=&charStr; T->ptr=store(); if(ptest(T->ptr)==1) { printf("\nThe entered string is Palindrome."); printf("\nEnter a character to search for: "); T->ch=getchar(); T->blk=getchar(); if(esearch(T->ptr,T->ch)) { printf("\nThe given character is found."); printf("\nEnter the replacing character: "); T->rh=getchar(); T->blk=getchar(); replace(T->ptr,T->ch,T->rh); printf("\nFinal String: "); fputs(T->ptr,stdout); } else {

printf("\nThe given character is not found."); } } else { printf("\nThe entered string is not Palindrome.\n"); printf("\nReverse String: %s",reverse(T->ptr)); } return 0; }//end of main function char *store() //Function to Read String and return pointer to first element { char *ptr; ptr=(char*)malloc(MAX*sizeof(char)); printf("Input String: "); fgets(ptr,MAX,stdin); return ptr; } //Fuction to check for palindrome int ptest(char *str) { int l,i; l=strlen(str);

l=l-1; // Using fgets() one \n is added for(i=0;i<=l/2;i++) { if(str[i]!=str[l-i-1]) return 0; } return 1; } //Function to search for a given character int esearch(char *str,char ch) { int i=0; while(*(str+i)!='\n') { if(*(str+i)==ch) return 1; i++; } return 0; } //Function to replace a given character void replace(char *str,char ch,char rh) {

int i=0; while(*(str+i)!='\n') { if(*(str+i)==ch) *(str+i)=rh; i++; } } //Function to reverse a string char* reverse(char *str) { int l,i,j; char temp; l=strlen(str); j=l-2; for(i=0;i<l/2;i++) { temp=*(str+i); *(str+i)=*(str+j); *(str+j)=temp; j--; } return str; }

9. WAP using Pointer Notation to solve a problem with following specifications: Let multicall() be a function having two parameters, first one a pointer to a function, second a pointer to an integer. main() will read an integer and call multicall() by passing the address of the integer and the address of one of the four functions: digitcount(, ,paltest(), reverse() and primetest(). multicall() will call one of the four functions and return appropriate value to main(). Descriptions of the functions are as follows: digitcount() : Return number of digits in an integer. paltest() : Return 0 if integer is a palindrome , 1 otherwise. reverse() : Return reverse of a given integer. primetest() : Return 1 if integer is a prime, 0 otherwise. main() will print the following after reading an integer: A. number of digits in the number B. if the number is a palindrome or not C. reverse of the number D. if the given number is a prime or not. /*coded by: Himanshu Kumar Pandey Reg No.: 2012ca93 date: 09/11/12 */ #include<stdio.h> int multicall(int(*)(int),int*); //Function Prototypes int digitcount(int);

int paltest(int); int reverse(int); int primetest(int); int main() //Main Function { typedef struct temp //Structure Declaration { int n; }intTemp; intTemp T; printf("\nEnter an Integer Value: "); //Input scanf("%d",&(T.n)); printf("\nDigit Count: %d\n",multicall(digitcount,&(T.n))); //Function Call and Display if(!multicall(paltest,&(T.n))) printf("Number is Palindrome\n"); else printf("Number is Not Palindrome\n"); printf("\nReverse Digit: %d\n",multicall(reverse,&(T.n))); if(multicall(primetest,&(T.n))) printf("Number is Prime\n"); else printf("Number is Not Prime\n");

return 0; }//end of main function //Function to call other functions int multicall(int (*fptr)(int),int *n) { return fptr(*n); } //Function to count the number of Digits int digitcount(int n) { int r=0; if(n==0) return 1; while(n>0) { r++; n/=10; } return r; } //Function to check whether its palindrome or not int paltest(int n) {

int nn=n,r,rev=0; while(nn>0) { r=nn%10; rev=rev*10+r; nn/=10; } if(rev==n) return 0; else return 1; } //Function to reverse a number int reverse(int n) { int r,rev=0; while(n>0) { r=n%10; rev=rev*10+r; n/=10; } return rev;

} //Function to check for prime int primetest(int n) { int i,flag=0; for(i=2;i<n;i++) { if(n%i==0) flag=1; } if(flag==0) return 1; else return 0; }

You might also like