Sie sind auf Seite 1von 46

SERIAL NO.

1 2 3

PROGRAMS Write a program to find out largest of three numbers. Write a program to reverse an integer and check whether the integer is palindrome or not. In a banking system there is denomination of notes e.g. Rs 100 to 1 Rs. Write a program which will accept an amount and find the denominations of notes that will combine the amount. In a hotel, the rooms are sequentially numbered from 1 to n. Prof. X is living in a room. The sum of room numbers to the left of Prof. Xs room is equal to the sum of room numbers of right of Prof. Xs room. Write a program to check the room no of Prof. X where n is taken by input. A pair of numbers is called amicable if the sum of the factors of the first number (excluding the number) is equal to the sum of the factors of the second number (excluding the number). Write a program to check whether a pair of number is amicable or not. Write a program to find out the sum of digits of an integer Write a program to find out whether a number is prime or not.

PAGE NO.

6 7 8 Write a program to generate prime numbers between m and n where n>m and m and n are integers.

INDEX 2
1

SERIAL NO. 1 2 3 4 5 6

PROGRAMS Write a program for searching an element in an array of integer using function. Write a program to find maximum and minimum number of an array using function. Write a program to sort an array of integers using user define function. Write a program to find the frequency of the numbers in an array. Write a program to delete duplicate elements from an array. Write a program using function to insert an element in a sorted array and ensure that after insertion the array remain sorted. Write a program to exchange odd positioned element with even positioned. Write a program which will read a 2-D matrix and compute norms of a matrix using function. Write a program to add two matrices and store the value in another matrix using 3 users define function. Write a program to multiply two matrices and store the value in another matrix using function. Write a program on transposition of a matrix using function. Write a program to check whether two matrices are symmetric or not using function. Write a program to compute saddle point of a matrix. Write a program to check whether a matrix is magic square or not.

PAGE NO.

7 8 9 10 11 12 13 14

INDEX 3
SERIAL NO. PROGRAMS PAGE NO. 2

Write a program to reverse a string using recursion. 1 2 3 4 5 6 7 Write a user function for string concatenation. 8 9 10 11 12 13 14 Write a user function to compare 2 strings. Write a user function to find the length of a string. Write a user function to copy one string to another string. Write a user function to return a substring from a string. Write a user function to find the position of a substring in a string. Write a program that compares two given dates. To store date using structure say date that contains three members namely date, month and year. If dates are equal then display message as Equal otherwise Unequal. Write a program to read the contents of a text file and print the words which are palindrome. Write a program to compute the prime factors of a number using recursion. Write a program to compute the sum of an array of integers using recursion. Write a program to compute the sum of digits of a number using recursion. Write a program compute the value of xn using recursion. Write a program to find out the factorial of a number using recursion. Write a program to compute nCr using recursion.

15

ASSIGNMENT-1

1. Program to find out largest of three numbers: 3

#include<stdio.h> main() { int a,b,c,d; clrscr(); printf("Enter 3 nos.:\n"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) d=a; else d=c; } else { if(b>c) d=b; else d=c; } printf("Largest no. is %d",d); getch(); }

2. Program to reverse an integer and check whether the integer is palindrome or not:

#include<stdio.h> main() 4

{ int n,s,n1; int d; clrscr(); printf("Enter a no.: "); scanf("%d",&n); s=0; n1=n; while(n!=0) { d=n%10; n=n/10; s=s*10+d; } if(n1==s) printf("\nNo. is palindrome"); else printf("\nNo. is not palindrome"); getch(); }

3. In a banking system there is denomination of notes e.g. Rs 100 to 1 Rs. Write a program which will accept an amount and find the denominations of notes that will combine the amount:

#include<stdio.h> main() { double amt=0; int h,f,t,tn,fv,tw,o,fp,tp,twp,tnp,top,op; clrscr(); printf("Enter amount:"); scanf("%lf",&amt); h=amt/100; if(h!=0)printf("\nHundred rupee notes: %d",h); amt-=h*100; f=amt/50; if(f!=0)printf("\nFifty rupee notes: %d",f); amt-=f*50; t=amt/20; if(t!=0)printf("\nTwenty rupee notes: %d",t); amt-=t*20; tn=amt/10; if(tn!=0)printf("\nTen rupee notes: %d",tn); amt-=tn*10; fv=amt/5; if(fv!=0)printf("\nFive rupee notes: %d",fv); amt-=fv*5; tw=amt/2; if(tw!=0)printf("\nTwo rupee notes: %d",tw); amt-=tw*2; o=amt/1; if(o!=0)printf("\nOne rupee notes: %d",o); getch(); }

4. In a hotel, the rooms are sequentially numbered from 1 to n. Prof. X is living in a room. The sum of room numbers to the left of Prof. Xs room is equal to the sum of room numbers of right of Prof. Xs room. Write a program to check the room no of Prof. X where n is taken by input: 6

#include<stdio.h> void main() { int flag=0; long int n,i,j,k,af=0,bf=0,num; printf("Enter the value of n"); scanf("%ld",&num); for(n=2;n<=num;n++) { for(i=1;i<=n;i++) { af=bf=0; for(j=1;j<i;j++) bf+=j; for(k=i+1;k<=n;k++) af+=k; if(bf==af) { flag=1; printf("\n For the value %ld the room no is %ld",n,i); } } } if(flag==0) printf("\n No such values found"); getch(); }

5. A pair of numbers is called amicable if the sum of the factors of the first number (excluding the number) is equal to the sum of the factors of the second number (excluding the number). Write a program to check whether a pair of number is amicable or not: 7

#include<stdio.h> main() { int a,b,i,sfa,sfb; clrscr(); printf("Enter 2 nos:\n"); scanf("%d%d",&a,&b); sfa=sfb=0; for(i=1;i<=a/2;i++) if(a%i==0) sfa=sfa+i; for(i=1;i<=b/2;i++) if(b%i==0) sfb=sfb+i; if(sfa==b && sfb==a) printf("The pair of nos. is Amicable."); else printf("The pair of nos. is not Amicable."); getch(); }

6. Program to find out the sum of digits of an integer:

#include<stdio.h> 8

main() { int n,d,s; clrscr(); printf("Enter a no.: "); scanf("%d",&n); s=0; while(n>0) { d=n%10; n=n/10; s=s+d; } printf("Sum of digits= %d",s); getch(); }

7. Program to find out whether a number is prime or not:

#include<stdio.h> main() { int n,i,flag=0; printf("Enter a no.\n"); scanf("%d",&n); for(i=2;i<n;i++) { if(n%i==0) { flag=1; break; } } if(flag==1) printf("\n%d is not a prime no.",n); else printf("\n%d is a prime no.",n); }

8. Program to generate prime numbers between m and n where n>m and m and n are integers:

10

#include<stdio.h> main() { int i,j,m,n,flag=0; printf("Enter the values of m and n---\n"); scanf("%d %d",&m,&n); if(n<m) { printf("n cannot be less than m"); } else { if(m==1) m++; printf("\nThe prime nos are----\n"); for(j=m;j<=n;j++) { for(i=2;i<j;i++) { if(j%i==0) { flag=1; break; } } if(flag==0) printf("%d ,",j); flag=0; } } }

ASSIGNMENT-2

1. Program for searching an element in an array of integer using function:

11

#include<stdio.h> #include<conio.h> void srch(int a[],int num1,int n1); void main() { int i,n,num,arr[100]; clrscr(); printf ("Enter the numbers of elements in the array: "); scanf("%d",&n); for(i=0;i<n;i++) { printf ("\n Enter no. %d:",i+1); scanf ("%d", &arr[i]); } printf ("\nEnter the number to be searched : "); scanf ("%d", &num); srch(arr,num,n); getch(); } void srch(int a[],int num1,int n1) { int j,flag; flag=0; for(j=0;j<n1;j++) { if(a[j]==num1) { flag=1; break; } } if(flag==1) printf("The number is found at position %d. ",j+1); else printf(" The number is not in the list." ); }

2. Program to find maximum and minimum number of an array using function:

#include <stdio.h> int max_min(int a[],int n,int *min); 12

void main() { int i,n,max,min,arr[100]; clrscr(); printf ("Enter the numbers of elements in the array: "); scanf("%d",&n); for(i=0;i<n;i++) { printf ("\n Enter no. %d:",i+1); scanf ("%d", &arr[i]); } min=arr[0]; max=max_min(arr,n,&min); printf("\nMaximum no. = %d",max); printf("\nMinimum no. = %d",min); getch(); } int max_min(int a[],int n,int *min) { int max,i; max=a[0]; for(i=1;i<n;i++) { if(a[i]>max) max=a[i]; if(a[i]<*min) *min=a[i]; } return max; }

3. Program to sort an array of integers using user define function:

# include <stdio.h> 13

void bsort(int a[],int n); void main() { int i,j,n; int arr[ 100 ]; clrscr(); printf ("How many numbers do you want to enter= "); scanf("%d",&n); printf("Enter the elements in the array:\n"); for( i = 0; i < n; i ++ ) { printf (" \n Enter the element %d :",i+1); scanf (" %d", &arr[ i ] ); } bsort(arr,n); getch(); } void bsort(int a[],int n) { int i,j,tmp; for( i = 0; i < n; i ++ ) { for( j = 0; j < n-1- i; j ++ ) { if( a[ j ] > a[ j + 1 ] ) { tmp =a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = tmp; } } } printf ( " Array in ascending order is: \n " ); for( i = 0; i < n; i ++ ) { printf( " %d " , a [ i ] ); } }

4. Program to find the frequency of the numbers in an array:

14

#include <stdio.h> main() { int i,j,c,arr[100],flag; clrscr(); printf (Enter the numbers of elements in the array: ); scanf(%d\n,&n); for(i=0;i<n;i++) { printf (\n Enter no. %d:,i+1); scanf (%d, &arr[i]); } for(i=0;i<n;i++) { flag=0; for(j=0;j<i;j++) if(arr[i]==arr[j]) { flag=1; break; } if(flag==0) { c=0; for(j=i;j<n;j++) if(arr[i]==arr[j]) c++; printf("Frequency of element %d is %d",arr[i],c); } } getch(); }

5. Program to delete duplicate elements from an array:

15

#include <stdio.h> main() { int i,j,k,n,arr[100],flag; clrscr(); printf ("Enter the numbers of elements in the array:" ); scanf("%d",&n); for(i=0;i<n;i++) { printf ("\n Enter no. %d:",i+1); scanf ("%d", &arr[i]); } for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(arr[i]==arr[j]) { for(k=j;k<n-1;k++) arr[k]=arr[k+1]; n--; j--; } printf("\nArray after duplicates removal:\n"); for(i=0;i<n;i++) printf("%d ",arr[i]); getch(); }

6. Program using function to insert an element in a sorted array and ensure that after insertion the array remain sorted:

#include <stdio.h> void bsort(int a[],int n); 16

void insert(int a[],int n,int num); void main() { int i,j,n,num; int arr[ 100 ]; clrscr(); printf ("How many numbers do you want to enter= "); scanf("%d",&n); printf("Enter the elements in the array:\n"); for( i = 0; i < n; i ++ ) { printf (" \n Enter the element %d :",i+1); scanf (" %d", &arr[ i ] ); } bsort(arr,n); printf("\nEnter the elementto be inserted: "); scanf("%d",&num); insert(arr,n,num); getch(); } void bsort(int a[],int n) { int i,j,tmp; for( i = 0; i < n; i ++ ) { for( j = 0; j < n-1- i; j ++ ) { if( a[ j ] > a[ j + 1 ] ) { tmp =a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = tmp; } } } printf ( " Array in ascending order is: \n " ); for( i = 0; i < n; i ++ ) { printf( " %d " , a [ i ] ); } } void insert(int a[],int n,int num) { int i,j; for(i=0;i<n;i++) if(num<a[i]) { for(j=n+1;j>i;j--) a[j]=a[j-1]; 17

a[i]=num; n++; break; } printf ( " \nArray after insertion: \n " ); for( i = 0; i < n; i ++ ) { printf( " %d " , a [ i ] ); } }

7. Program to exchange odd positioned element with even positioned:

#include<stdio.h> 18

void main() { int a[20],i,j,t=0,n; clrscr(); printf("enter the length of the array"); scanf("%d",&n); printf("enter the elements of the array"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("The array is"); for(i=0;i<n;i++) { printf("%d ",a[i]); } for(i=0;i<n;i=i+2) { if(i!=n-1) { j=i+1; t=a[i]; a[i]=a[j]; a[j]=t; } } printf("The new array is"); for(i=0;i<n;i++) { printf("%d ",a[i]); } getch(); }

8. Program which will read a 2-D matrix and compute norms of a matrix using function:

19

#include<stdio.h> int norm(int a[][20],int m,int n); void main() { int arr[20][20],m,n,i,j; int s; clrscr(); printf("Enter the values of m & n: "); scanf("%d%d",&m,&n); printf("Enter the elements:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("arr[%d][%d]: ",i,j); scanf("%d",&arr[i][j]); } } s=norm(arr,m,n); printf("Norms=%d",s); getch(); } int norm(int a[][20],int m,int n) { int s; int i,j; s=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { s+=a[i][j] * a[i][j]; } } return s; }

9. Program to add two matrices and store the value in another matrix using 3 users define function:

20

#include<stdio.h> void input(int a[][20],int m,int n); void add(int a[][20],int b[][20],int c[][20],int m,int n); void display(int a[][20],int m, int n); void main() { int a[20][20],b[20][20],c[20][20],m,n; clrscr(); printf("Enter the values of m & n(max 20): "); scanf("%d%d",&m,&n); printf("Enter the elements:\n"); input(a,m,n); printf("First matrix:\n"); display(a,m,n); printf("Enter the elements of second matrix:\n"); input(b,m,n); printf("Second matrix:\n"); display(b,m,n); printf("Matrix after addition:"); add(a,b,c,m,n); display(c,m,n); getch(); } void input(int a[][20],int m,int n) { int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) { printf("a[%d][%d]: ",i,j); scanf("%d",&a[i][j]); } } void add(int a[][20],int b[][20],int c[][20],int m, int n) { int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; } } void display(int a[][20],int m,int n) { int i,j; 21

printf("\nResultant Matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",a[i][j]); printf("\n"); } }

10. Program to multiply two matrices and store the value in another matrix using function: 22

#include<stdio.h> void input(int a[][20],int m,int n); void mult(int a[][20],int b[][20],int c[][20],int m, int n,int p); void display(int a[][20],int m, int n); void main() { int a[20][20],b[20][20],c[20][20],m1,n1,n2; clrscr(); printf("Enter the values of m1 & n1(max 20): "); scanf("%d%d",&m1,&n1); printf("Enter the elements:\n"); input(a,m1,n1); printf("First matrix:\n"); display(a,m1,n1); printf("Enter the values of n2(max 20): "); scanf("%d",&n2); printf("Enter the elements of second matrix:\n"); input(b,n1,n2); printf("Second matrix:\n"); display(b,n1,n2); printf("Matrix after multiplication:"); mult(a,b,c,m1,n1,n2); display(c,m1,n2); getch(); } void input(int a[][20],int m,int n) { int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) { printf("a[%d][%d]: ",i,j); scanf("%d",&a[i][j]); } } void mult(int a[][20],int b[][20],int c[][20],int m, int n,int p) { int i,j,k,s; for(i=0;i<m;i++) for(j=0;j<p;j++) { s=0; for(k=0;k<n;k++) s=s+a[i][k]*b[k][j]; 23

c[i][j]=s; } } void display(int a[][20],int m,int n) { int i,j; printf("\nResultant Matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",a[i][j]); printf("\n"); } }

11. Program on transposition of a matrix using function:

#include<stdio.h> void transpose(int a[][20],int m,int n); void main() { 24

int a[20][20],i,j,m,n; clrscr(); printf("Enter the rows and columns of the matrix:\n"); scanf("%d%d",&m,&n); printf("Enter the elements of the matrix:\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) { printf("a[%d][%d]=",i,j); scanf("%d",&a[i][j]); } printf("The matrix is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%5d",a[i][j]); } printf("\n"); } transpose(a,m,n); getch(); } void transpose(int a[][20],int m,int n) { int i,j,b[20][20]; for(i=0;i<m;i++) for(j=0;j<n;j++) { b[j][i]=a[i][j]; } printf("The transposed matrix is:\n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) { printf("%5d",b[i][j]); } printf("\n"); } } 12. Program to check whether two matrices are symmetric or not using function: #include<stdio.h> int symmetric(int a[][20],int m,int n); void main() { 25

int a[20][20],i,j,m,n,f; f=0; clrscr(); printf("Enter the rows and columns of the matrix:\n"); scanf("%d%d",&m,&n); printf("Enter the elements of the matrix:\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) { printf("a[%d][%d]=",i,j); scanf("%d",&a[i][j]); } printf("The matrix is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%5d",a[i][j]); } printf("\n"); } f=symmetric(a,m,n); if(f==1) printf("\nThe matrix is not symmetric."); else printf("\nThe matrix is symmetric."); getch(); } int symmetric(int a[][20],int m,int n) { int i,j,f=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(a[i][j]!=a[j][i]) { f=1; break; } } } return f; } 13. Program to compute saddle point of a matrix:

#include<stdio.h> void saddle(int a[100][100],int,int); void main() { 26

int a[100][100],m,n,i,j; printf("Enter dimensions of matrix:\n"); scanf("%d%d",&m,&n); printf("Enter elements:\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) { printf("a[%d][%d]: ",i,j); scanf("%d",&a[i][j]); } printf("Matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",a[i][j]); printf("\n"); } saddle(a,m,n); } void saddle(int a[100][100],int m,int n) { int i,j,k,flag,flag1,flag2,max,min; flag=0; for(i=0;i<m;i++) for(j=0;j<n;j++) { flag1=flag2=0; max=min=a[i][j]; for(k=0;k<n;k++) { if(a[i][k]<min) min=a[i][k]; if(a[i][k]>max) max=a[i][k]; } if(min==a[i][j]) flag1=1; if(max==a[i][j]) flag1=2; max=min=a[i][j]; for(k=0;k<m;k++) { if(a[k][j]<min) min=a[k][j]; if(a[k][j]>max) max=a[k][j]; } if(min==a[i][j]) flag2=1; if(max==a[i][j]) 27

flag2=2; if(flag1>0 && flag2>0 && flag1!=flag2) { flag=1; printf("Saddle point at (%d,%d) is %d\n",i,j,a[i][j]); } } if(flag==0) printf("No saddle point\n"); }

14. Program to check whether a matrix is magic square or not:

#include<stdio.h> #include<conio.h> 28

void main() { int a[100][100],n,i,j,s,s1,s2,flag; clrscr(); printf("Enter value of n:\n"); scanf("%d",&n); printf("Enter values:\n"); for(i=0;i<n;i++) for(j=0;j<n;j++) { printf("a[%d][%d]: ",i,j); scanf("%d",&a[i][j]); } s=0; for(j=0;j<n;j++) s=s+a[0][j]; flag=0; for(i=0;i<n;i++) { s1=s2=0; for(j=0;j<n;j++) { s1=s1+a[i][j]; s2=s2+a[j][i]; } if(s1!=s || s2!=s) { flag=1; break; } } if(flag==0) { s1=s2=0; for(i=0;i<n;i++) { s1=s1+a[i][i]; s2=s2+a[i][n-1-i]; } if(s1!=s || s2!=s) { flag=1; } } if(flag==0) printf("Entered Matrix is a Magic Square!!"); else printf("Entered Matrix is not a Magic Square!!"); getch(); } 29

ASSIGNMENT-3

1. Program to reverse a string using recursion:

30

#include<stdio.h> #include<conio.h> #include<string.h> void str_rev(char a[],char b[],int,int); void main() { char a[100],b[100]; int l; clrscr(); printf("Enter a string: "); scanf("%s",&a); l=strlen(a); str_rev(a,b,l-1,0); puts(b); getch(); } void str_rev(char a[],char b[],int l,int i) { if(l<0) { b[i]='\0'; return; } b[i++]=a[l--]; str_rev(a,b,l,i); }

2. Program to find out the factorial of a number using recursion:

#include<stdio.h> #include<conio.h> int fact(int); void main() 31

{ int n,f; clrscr(); printf("Enter no.: "); scanf("%d",&n); f=fact(n); printf("Factorial= %d",f); getch(); } int fact(int n) { if(n==1) return 1; else return n*fact(n-1); }

3. Program to compute nCr using recursion:

#include<stdio.h> #include<conio.h> int fact(int); void main() 32

{ int n,r,ncr,nf,rf,nrf; clrscr(); printf("Enter n: "); scanf("%d",&n); printf("Enter r: "); scanf("%d",&r); nf=fact(n); rf=fact(r); nrf=fact(n-r); ncr=nf/(rf*nrf); printf("Factorial= %d",ncr); getch(); } int fact(int n) { if(n==1) return 1; else return n*fact(n-1); }

4. Program to compute the prime factors of a number using recursion:

#include<stdio.h> #include<conio.h> int n; void pf(int); 33

void main() { printf("Enter no.: "); scanf("%d",&n); pf(2); getch(); } void pf(i) { if(n%i==0) { printf("%d ",i); n=n/i; } else i++; if(n==1) return; pf(i); }

5. Program to compute the sum of an array of integers using recursion:

#include<stdio.h> #include<conio.h> int soa(int arr[],int); main() 34

{ int i,n,arr[100],s; clrscr(); printf ("Enter the numbers of elements in the array: "); scanf("%d",&n); for(i=0;i<n;i++) { printf ("\n Enter element %d:",i+1); scanf ("%d", &arr[i]); } s=soa(arr,n-1); printf("Sum of integers= %d",s); getch(); } int soa(int a[],int n) { if(n==0) return a[n]; else return a[n]+soa(a,n-1); }

6. Program to compute the sum of digits of a number using recursion:

#include<stdio.h> #include<conio.h> int sod(int); void main() 35

{ int n,s; printf("Enter value for n: "); scanf("%d",&n); s=sod(n); printf("Sum of digits= %d",s); getch(); } int sod(int n) { int d; if(n==0) return 0; d=n%10; return d+sod(n/10); }

7. Program to compute the value of xn using recursion:

#include<stdio.h> 36

#include<conio.h> int x_n(int,int); void main() { int x,n,xn; clrscr(); printf("Enter x: "); scanf("%d",&x); printf("Enter n: "); scanf("%d",&n); xn=x_n(x,n); printf("x^n= %d",xn); getch(); } int x_n(int x,int n) { if(n==1) return x; else return x*x_n(x,n-1); }

8. User function for string concatenation:

#include<stdio.h> 37

#include<string.h> void usrstrcat(char *p,char *q); void main() { char a[30],b[30]; clrscr(); printf("Enter first string:"); gets(a); printf("\nEnter second string:"); gets(b); usrstrcat(a,b); printf("\nThe string after concatination is %s.",a); getch(); } void usrstrcat(char *p,char *q) { char *r; r=p; while(*p!=NULL) p++; while(*q!=NULL) { *p=*q; p++; q++; } *p=NULL; p=r; }

9. User function to compare 2 strings:

#include<stdio.h> #include<string.h> int usrstrcmp(char *p,char *q); void main() 38

{ char a[20],b[20]; int c; clrscr(); printf("Enter first string:"); gets(a); printf("\nEnter second string:"); gets(b); c=usrstrcmp(a,b); if(c==0) printf("\nThe two string %s and %s are same.",a,b); else printf("\nThe two string %s and %s are not same.",a,b); getch(); } int usrstrcmp(char *p,char *q) { int x=0; while(*p!=NULL || *q!=NULL) { if(*p!=*q) { if((int)*p>(int)*q) { x=1; break; } else { x=-1; break; } } p++; q++; } return x; }

10. User function to find the length of a string:

#include<stdio.h> #include<string.h> int usrstrlen(char *p); 39

void main() { char c[20]; int l; clrscr(); printf("Enter the string: "); gets(c); l=usrstrlen(c); printf("\nThe length of %s is %d",c,l); getch(); } int usrstrlen(char *p) { int l=0; while(*p!=NULL) { l++; p++; } return l; }

11. User function to copy one string to another string:

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

void usrstrcpy(char *p,char *q); void main() { char a[20],b[20],c[20]; clrscr(); printf("Enter first string:"); gets(a); usrstrcpy(c,a); printf("\nEnter your second string:"); gets(b); usrstrcpy(a,b); printf("\nString %s change to %s after copying.",c,a); getch(); } void usrstrcpy(char *p,char *q) { char *r; r=q; while(*q!=NULL) { *p=*q; p++; q++; } *p=NULL; p=r; }

12. User function to return a substring from a string:

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

void usrsubstr(char *p,char *q,int m,int n); void main() { char a[20],b[20]; int m,n; clrscr(); printf("Enter the string: "); gets(a); printf("\nEnter the first position:"); scanf("%d",&m); printf("\nEnter the second position:"); scanf("%d",&n); if(n>=m) { if((m<0)||(n>strlen(a))) printf("\nGiven position are out of bound."); else usrsubstr(a,b,m,n); puts(b); } else printf("\nSarting position must be less than or equal to ending position!!!!"); getch(); } void usrsubstr(char *p,char *q,int m,int n) { char *r; int i; r=q; p=p+m; for(i=0;i<n-m;i++) { *q=*p; p++; q++; } *q=NULL; q=r; }

13. User function to find the position of a substring in a string:

#include<stdio.h> 42

#include<string.h> int usrindexof(char *a,char *b,int p); void main() { char a[20],b[20],pos,p=0; clrscr(); printf("Enter the string:"); gets(a); printf("\nEnter the substring:"); gets(b); pos=usrindexof(a,b,p); if(pos==-1) printf("\nNo match found."); else printf("\nSubstring found at %dth position.",pos+1); getch(); } int usrindexof(char *a,char *b,int p) { int flag=0,c; if(*a!=*b) { p++; a++; if(*a==NULL) return -1; else return usrindexof(a,b,p); } else{ c=0; while(*b!=NULL) { if(*b!=*a) { flag=1; break; } c++; a++; b++; } if(flag==0) return p; else { p++; b=b-c; a=a-c+1; 43

if(*a!=NULL) return usrindexof(a,b,p); else return -1; } } }

14. Program that compares two given dates.To store date using structure say date that contains three members namely date, month and year. If dates are equal then display message as Equal otherwise Unequal.

#include<stdio.h> #include<conio.h> 44

struct date { int dd; int mm; int yy; }; void main() { struct date d1,d2; clrscr(); printf("Enter date 1:\nDay: "); scanf("%d",&d1.dd); printf("Month: "); scanf("%d",&d1.mm); printf("Year: "); scanf("%d",&d1.yy); clrscr(); printf("Enter date 2:\nDay: "); scanf("%d",&d2.dd); printf("Month: "); scanf("%d",&d2.mm); printf("Year: "); scanf("%d",&d2.yy); clrscr(); if(d1.dd==d2.dd && d1.mm==d2.mm && d1.yy==d2.yy) printf("Equal"); else printf("Unequal"); getch(); }

15. Read the contents of a text file and print the words which are palindrome:

#include<stdio.h> #include<conio.h> #include<string.h> void str_rev(char a[],char b[],int,int); 45

void main() { char c,a[30],b[30],file1[100]; int l; FILE *fp; clrscr(); printf("Enter file name: "); scanf("%s",file1); fp=fopen(file1,"r"); printf("\nPalindrome words:\n"); while(fscanf(fp,"%c",&c)>0) { if(c!=' ') a[l++]=c; else { a[l]='\0'; str_rev(a,b,l-1,0); if(strcmp(a,b)==0) printf("%s\n",a); l=0; } } fcloseall(); getch(); } void str_rev(char a[],char b[],int l,int i) { if(l<0) { b[i]='\0'; return; } b[i++]=a[l--]; str_rev(a,b,l,i); }

46

Das könnte Ihnen auch gefallen