You are on page 1of 20

4/10/2014

Share

ccp lab manual vtu 1sem


0

More

Next Blog

Create Blog

Sign In

ccp lab manual vtu 1sem


mahesh m
Follow

WEDNESDAY, 30 OCTOBER 2013

ccp lab manual


SHARE IT

pow ered by

Share this on Facebook Tweet this


View stats
(NEW) Appointment gadget >>

ccp lab manual preferred by vtu

TOTAL PAGEVIEWS

6 9 8
10CPL16/26 COMPUTER PROGRAMMING LABORATORY PART A 1. Design, develop and execute a program in C to find and output all the roots of a given quadratic equation, for non-zero coefficients. 2. Design, develop and execute a program in C to implement Euclids algorithm to find the GCD and LCM of two integers and to output the results along with the given integers. 3. Design, develop and execute a program in C to reverse a given four digit integer number and check whether it is a palindrome or not. Output the given number with suitable message. 4. Design, develop and execute a program in C to evaluate the given polynomial f(x) = a4x4 + a3x3 + a2x2 + a1x1 + a0 for given value of x and the coefficients using Horners method. 5. Design, develop and execute a program in C to copy its input to its output, replacing each string of one or more blanks by a single blank. 6. Design, develop and execute a program in C to input N integer numbers in ascending order into a single dimension array, and then to perform a binary search for a given key integer number and report success or failure in the form of a suitable message. 7. Design, develop and execute a program in C to input N integer numbers into a single dimension array, sort them in to ascending order using bubble sort technique, and then to print both the given array and the sorted array with suitable headings. 8. Design, develop and execute a program in C to compute and print the word length on the host machine. PART B 9. Design, develop and execute a program in C to calculate the approximate value of exp (0.5) using the Taylor Series expansion for the exponential function. Use the terms in the
http://ccp-lab-manual.blogspot.in/
BLOG ARCHIVE

2013 (1) October (1) ccp lab manual

ABOUT ME

mahesh m
Follow

20

View my complete profile

1/20

4/10/2014

ccp lab manual vtu 1sem

expansion until the last term is less than the machine epsilon defines as FLT_EPSILON in the header file <float.h>. Print the value returned by the Mathematical function exp ( ) also. 10. Design, develop and execute a program in C to read two matrices A (M x N) and B (P x Q) and to compute the product of A and B if the matrices are compatible for multiplication. The program is to print the input matrices and the resultant matrix with suitable headings and format if the matrices are compatible for multiplication, otherwise the program must print a suitable message.(For the purpose of demonstration, the array sizes M, N, P, and Q can all be less than or equal to 3) Computer Programming Lab Manual Noor-E-Saba, Dept. of CSE@HKBKCE Page 2 11. Design, develop and execute a parallel program in C to add, element-wise, two onedimensional arrays A and B of N integer elements and to store the result in another onedimensional array C of N integer element. 12. Design and develop a function rightrot(x, n) in C that returns the value of the integer x rotated to the right by n bit positions as an unsigned integer. Invoke the function from the main with different values for x and n and print the results with suitable headings. 13. Design and develop a function isprime(x) that accepts an integer argument and returns 1 if the argument is prime and 0 otherwise. The function is to use plain division checking approach to determine if a given number is prime. Invoke this function from the main with different values obtained from the user and print appropriate messages. 14. Design, develop and execute a parallel program in C to determine and print the prime numbers which are less than 100 making use of algorithm of the Sieve of Eratosthenes. 15. Design and develop a function reverses(s) in C to reverse the string s in place. Invoke this function from the main for different strings and print the original and reversed strings. 16. Design and develop a function matchany (s1,s2) which returns the first location in the string s1 where any character from the string s2 occurs, or 1 if s1 contains no character from s2. Do not use the standard library function which does a similar job! Invoke the function matchany (s1. s2) from the main for different strings and print both the strings and the return value from the function matchany (s1, s2). 1. Note: In the practical examination the student has to answer two questions. One question from Part A and one question from Part B, will be selected by the student by lots. All the questions listed in the syllabus have to be included in the lots. The change of question (Part A only / Part B only / Both Part A & Part B) has to be considered, provided the request is made for the same, within half an hour from the start of the examination. The allotment of marks is as detailed below: Sl. # Activity Max. Marks 1 Procedure Writing program & procedure for the assigned problems along with algorithms / flowchart Part A 5* Part B 5* 2 Conduction Execution of the program and showing the results in proper format Part A 10 Part B 20 3 Viva-voce** 10 Total Max. Marks 50 Minimum Passing Marks (40% of Max. Marks) 20 PART A 1) Design, develop and execute a program in C to find and output all the roots of
http://ccp-lab-manual.blogspot.in/ 2/20

4/10/2014

ccp lab manual vtu 1sem

a given quadratic equation,for non-zero co-efficient Algorithm 1. Start. 2. Input co-efficient of equation a, b, c . 3. IF any or all the coefficients are zero Print Invalid input ELSE d b2- 4ac r |d| IF d > 0 r1 (-b +r)/ (2a) r2 (-b -r)/ (2a) Print Roots are REAL and DISTINCT Print r1, r2 ELSE IF d < 0 r1 -b/ (2a) r2 r/ (2a) Print Roots are COMPLEX Print r1 +i r2, r1 - i r2 ELSE r1-b/(2a) Print Roots are EQUAL Print r1, r1 END IF END IF END IF. 4. Stop

Program #include <stdio.h> #include <math.h> void main() { int a,b,c; float d,x1,x2,r; printf("Enter the three co-efficient :\n"); scanf("%d%d%d",&a,&b,&c); if (a* b* c == 0) { printf("\n Invalid Input "); } else { d = b * b - 4 * a * c; r=sqrt(fabs(d)); if (d > 0) { x1 = (-b +r) / (2.0*a); x2 = (-b -r) / (2.0*a); printf("\n The roots are real and distinct\n"); printf("\n The roots are \n 1) x1=%f\t\t \n 2) x2=%f",x1,x2); } else if (d == 0) { x1 = x2 = -b/(2.0*a); printf("\n The roots are real and equal\n"); printf("\n The roots are: \n 1) x1=x2=%f",x1); } else
http://ccp-lab-manual.blogspot.in/ 3/20

4/10/2014

ccp lab manual vtu 1sem

{ x1 = -b / (2.0 * a); x2 = r / (2.0*a); printf("\n The roots are real and imaginary\n"); printf("\n The roots are:\n 1) %f +i %f \t\t\n 2) %f i %f ",x1,x2,x1,x2); } } } Sample Output 1. Enter the three co-efficient : 144 The roots are real and equal The roots are: X1=X2=2.0000 2. Enter the three co-efficient : 1-56 The roots are real and distinct The roots are: X1=3.0000 X2=2.0000 3. Enter the three co-efficient : 234 The roots are real and imaginary The roots are: 1) -0.750000 +i 1.198958 2) -0.750000 - i 1.198958 4. Enter the three co-efficient : 105 Invalid Input 2) Design, develop and execute a program in C to implement Euclids algorithm to find the GCD and LCM of two integers and to output the results along with the given integers. Algorithm 1. Start. 2. Input m , n. 3. Initialize p m , q n. 4. Until n <> 0 rem m mod n mn n rem END until 5. GCD m 6. LCM p * q / GCD. 7. Print p, q, GCD, LCM. 8. Stop

Program #include<stdio.h> void main() { int m,n,p,q,gcd,lcm,rem; printf("Enter two numbers : "); scanf("%d%d",&m,&n); p = m; q = n;
http://ccp-lab-manual.blogspot.in/ 4/20

4/10/2014

ccp lab manual vtu 1sem

while(n!=0) { rem=m%n; m=n; n=rem; } gcd = m; lcm = (p * q) / gcd; printf("\n The LCM of %d and %d = %d",p,q,lcm); printf("\n The GCD of %d and %d = %d",p,q,gcd); } Sample Output Enter two numbers: 34 66 The LCM = 1122 The GCD = 2 3) Design, develop and execute a program in C to reverse a given four digit integer number and check whether it is a palindrome or not. Output the given number with suitable message. Algorithm 1. Start. 2. Input n. 3. Initialize a n, rev 0, rem 0. 4. IF n <= 999 OR n>9999 Print Not a 4 digit number Goto step 7. 5. Until n <>0 rem n % 10 rev rev *10 + rem. n n / 10. END until 6. IF a EQUAL TO rev Print Palindrome. ELSE Print Not a Palindrome. 7. Stop. Program #include <stdio.h> void main() { int n,rev=0,rem,a; printf("Enter a number : "); scanf("%d",&n); a = n; if(n<=999 || n>9999) { printf( Not a 4 digit number\n); exit(0); } while(n != 0) { rem=n%10; rev= rev*10+rem; n = n / 10; } if(a==rev) printf("\n The given Number %d is Palindrome",a);
http://ccp-lab-manual.blogspot.in/ 5/20

4/10/2014

ccp lab manual vtu 1sem

else printf("\n The given Number %d is not Palindrome",a); } Sample Output 1. Enter a number: 201 Not a four digit number 2. Enter a number: 5642 The Number is not Palindrome 3. Enter a number: 8118 The Number is Palindrome 4) Design, develop and execute a program in C to evaluate the given polynomial f(x)= a4x4 + a3x3 + a2x2 + a1x1 + a0 for given value of x and the coefficients using Horners method. Algorithm 1. Start. 2. Read n. 3. FOR i 0 to n in steps of 1 Read a[i] END FOR 4. Read x. 5. poly a[0] 6. FOR i 1 to n in steps of 1 poly poly * x + a[i] END FOR. 7. Print poly. 8. Stop. Program #include <stdio.h> void main() { int n,i,x,a[10],poly=0; printf("\n Enter the degree of the polynomial : "); scanf("%d",&n); printf("\n Enter the %d coefficients\n",n+1); for(i = 0 ; i <= n ; i++) { scanf("%d",&a[i]); } printf("\n Enter the value of x :"); scanf("%d",&x); poly=a[0]; for(i = 1 ; i <= n ; i++) { poly = poly* x+a[i]; } printf("\n The sum of polynomial = %d",poly); } Sample Output Enter the degree of the polynomial : 4 Enter the 5 coefficient 12345 Enter the value of x : 1 The sum of polynomial = 15
http://ccp-lab-manual.blogspot.in/ 6/20

4/10/2014

ccp lab manual vtu 1sem

5) Design, develop and execute a program in C to copy its input to its output, replacing each string of one or more blanks by a single blank. Algorithm 1. Start 2. Read the text in Array c 3. FOR i 0 to c [i] <> '\0' in steps of 1 IF c [i] = ' ' Print ' ' End If Until c [i] = \0' Increment i End Until Print the character End For 4. Stop Program #include <stdio.h> #include<string.h> void main() { char c[50]; int I; printf("Enter the text"); scanf(%[^\t\n], c); for(i=0;c[i]!='\0';i++) { if (c[i]==' ') printf(%c, c[i]); while (c[i]==' ') i++; printf(%c, c[i]); } } Sample output Enter the text welcome welcome 6) Design, develop and execute a program in C to input N integer numbers in ascending order into a single dimension array, and then to perform a binary search for a given key integer number and report success or failure in the form of a suitable message. Algorithm 1. Start. 2. Initialize Flag 0 3. Read n. 4. FOR i 0 to n-1 in steps of 1 Read numbers in ascending order in Array a[ ] END FOR. 5. Read key. 6. Initialize low0, highn -1. 7. Until low <= high mid(low + high)/ 2. 8. IF key = a[mid] Flag=1 Goto Step 10 END IF 9. IF key > a[mid] lowmid + 1
http://ccp-lab-manual.blogspot.in/ 7/20

4/10/2014

ccp lab manual vtu 1sem

ELSE highmid 1 END IF END Until 10. IF Flag = 1 Print Successful Search. ELSE Print Unsuccessful Search. END IF 11. Stop. program #include <stdio.h> void main() { int n,i,a[10],key,low,high,mid,Flag=0; printf("\n Enter the no. of elements : "); scanf("%d",&n); printf("\n Enter %d elements in ascending order ",n); for(i = 0 ; i < n ; i++) scanf("%d",&a[i]); printf("\n Enter the key element to search : "); scanf("%d",&key); low = 0; high = n-1; while(low <= high) { mid=(low+high)/2; if(key==a[mid]) { Flag=1; break; } if(key > a[mid]) low=mid+1; else high = mid-1; } if(Flag==1) printf("\n Successful Search "); else printf("\n Unsuccessful Search"); } Sample Output Enter the no. of elements: 5 Enter 5 elements in ascending order 10 20 30 40 50 Enter the key element to search: 30 Search Successful Enter the no. of elements: 5 Enter 5 elements in ascending order 2345 6 Enter the key element to search : 7 unsuccessful Search
http://ccp-lab-manual.blogspot.in/ 8/20

4/10/2014

ccp lab manual vtu 1sem

7) Design, develop and execute a program in C to input N integer numbers into a single dimension array, sort them in to ascending order using bubble sort technique, and then to print both the given array and the sorted array with suitable headings. Algorithm 1. Start. 2. Read n. 3. FOR i0 to n-1 in steps of 1 Read integer numbers in array a [ ] b[i]a[i] END FOR. 4. FOR i0 to n-1 in steps of 1 FOR j0 to n-i in steps of 1 IF a[j] > a[j + 1] temp= a[j] a[j] = a[j + 1] a[j+1] = temp END IF END FOR END FOR 5. Print original array b[i] FOR i0 to n-1 in steps of 1 Print b[i] END FOR. 6. Print sorted array a[i] FOR i0 to n-1 in steps of 1 Print a[i] END FOR. 7. Stop. Program #include <stdio.h> void main() { int n,i,j,a[10],b[10],temp; printf("\n Enter the no. of elements : "); scanf("%d",&n); printf("\n Enter %d elements ",n); for(i = 0 ; i < n ; i++) { scanf("%d",&a[i]); b[i]=a[i]; }f or(i = 0 ; i < n-1 ; i++) { for(j = 0 ; j < n-i; j++) { if(a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } printf("\n The original elements are\n "); for(i = 0 ; i < n ; i++) printf("%d \n",b[i]); printf("\n The Sorted elements are ");
http://ccp-lab-manual.blogspot.in/ 9/20

4/10/2014

ccp lab manual vtu 1sem

for(i = 0 ; i < n ; i++) printf("%d \n",a[i]); } Sample Output Enter the no. of elements : 5 Enter 5 elements 91 15 2 31 4 The original elements are 91 15 2 31 4 The Sorted elements are 131 4 52 91 8) Design, develop and execute a program in C to compute and print the word length on the host machine. Algorithm 1. Start 2. Initialize Var-1 Wordlen 0 3. Until (var) Wordlen++ Var<<1 End Until 4. Print The word length of host machine 5. Stop Program #include<stdio.h> void main() { int var = -1,wordlen=0; while (var) { wordlen++; var <<= 1; } printf(" The Word length of this Host Machine is %d Bits", wordlen); } Sample output The Word length of this Host Machine is 32 Bits PART-B 9) Design, develop and execute a program in C to calculate the approximate value of exp (0.5) using the Taylor Series expansion for the exponential function. Use the terms in the expansion until the last term is less than the machine epsilon defines as FLT_EPSILON in the header file <float.h>.Print the value returned by the mathematical function exp( ) also.
http://ccp-lab-manual.blogspot.in/ 10/20

4/10/2014

ccp lab manual vtu 1sem

Algorithm 1. Start 2. Read X 3. Initialize sum0,term1,fact1 4. for i1; term >= FLT_EPSILON; i++ fact fact*i sum sum+term term pow(x,i)/fact 5. Print calculated value 6. Print Library function value. 7. Stop Program #include<stdio.h> #include <float.h> #include <math.h> void main() { int i; float x,sum,fact,term; printf("\nYou have this series : 1+x/1!+x^2/2!+ x^3/3! + x^4/4!+.x^n/n!"); printf("\n\nEnter the value for X : "); scanf("%f",&x); sum = 0; term = 1; fact = 1; for(i=1;term >= FLT_EPSILON;i++) { fact =fact * i; sum = sum + term; term = pow(x,i)/fact; } printf("\n\nThe Calculated value of e^% .3f = %f",x,sum); printf("\n\nThe Library Function Value of e^%.3f = %f",x,exp(x)); } Sample Output You have this series: 1+x/1!+x^2/2!+x^3/3!+x^4/4!+.x^n/n! Enter the value for X : 0.5 The Calculated value of e^ 0.500 = 1.648721 The Library Function Value of e^ 0.500 = 1.648721 10) Design, develop and execute a program in C to read two matrices A(M*N) and B(P*Q) and to compute the product of A and B if the matrices are compatible for multiplication. The program is to print the input matrices and resultant matrix with suitable headings and format if the matrices are compatible for Multiplication. Otherwise the program must print a suitable message.(For the purpose of demonstration, the array sizes M,N,P and Q can all be less than or equal to 3). Algorithm 1. Start. 2. Read order m, n 3. Read order p, q 4. IF n==p THEN FOR i 0 to m in steps of 1 FOR j 0 to n in steps of 1 Read a[i][j] END FOR END FOR 5. FOR i 0 to p in steps of 1 FOR j 0 to q in steps of 1
http://ccp-lab-manual.blogspot.in/ 11/20

4/10/2014

ccp lab manual vtu 1sem

Read b[i][j] END FOR END FOR 6. Matrix multiplication FOR i 0 to m in steps of 1 FOR j 0 to q in steps of 1 c[i][j] 0 FOR k 0 to n in steps of 1 c[i][j] c[i][j] + a[i][k] * b[k][j] END FOR END FOR END FOR End if 7. Print matrix A FOR i 0 to m in steps of 1 FOR j 0 to n in steps of 1 Print a[i][j] END FOR END FOR 8. Print matrix B FOR i 0 to p in steps of 1 FOR j 0 to q in steps of 1 Print b[i][j] END FOR END FOR 9. Print matrix C sample out put FOR i 0 to m in steps of 1 FOR j 0 to q in steps of 1 Read c[i][j] END FOR END FOR ELSE Print Multiplication not possible END IF 10. Stop. Program #include <stdio.h> void main() { int a[10][10],b[10][10],c[10][10]; int m,n,p,q,i,j,k; printf("\n Enter the order of the matrix A :"); scanf("%d%d",&m,&n); printf("\n Enter the order of the matrix B :"); scanf("%d%d",&p,&q); if(n==p) { printf("\n Enter the elements of matrix A \n"); for(i = 0 ; i < m ; i++) { for(j = 0 ; j < n ; j++) scanf("%d",&a[i][j]); } printf("\n Enter the elements of matrix B \n"); for(i = 0 ; i < p ; i++) { for(j = 0 ; j < q ; j++) scanf("%d",&b[i][j]);
http://ccp-lab-manual.blogspot.in/ 12/20

4/10/2014

ccp lab manual vtu 1sem

}f or(i = 0 ; i < m ; i++) { for(j = 0 ; j < q ; j++) { c[i][j]=0; for(k = 0 ; k < n ; k++) c[i][j] += a[i][k] * b[k][j]; } } printf("\n MATRIX A \n"); for(i = 0 ; i < m ; i++) { for(j = 0 ; j < n ; j++) { printf(" %d \t", a[i][j]); } printf("\n"); } printf("\n MATRIX B \n"); for(i = 0 ; i < p ; i++) { for(j = 0 ; j < q ; j++) { printf(" %d \t", b[i][j]); } printf("\n"); } printf("\n MATRIX C \n"); for(i = 0 ; i < m ; i++) { for(j = 0 ; j < q ; j++) { printf(" %d \t", c[i][j]); } printf("\n"); }} else printf("Matrix A & B is not multiplicable"); } Sample Output Enter the order of the matrix A :2 2 Enter the order of the matrix B :2 2 Enter the elements of matrix A 12 34 Enter the elements of matrix B 23 45 MATRIX A 12 34 MATRIX B 23 45 MATRIX C 10 13 22 29 Enter the order of the matrix A :2 3 Enter the order of the matrix B :2 3
http://ccp-lab-manual.blogspot.in/ 13/20

4/10/2014

ccp lab manual vtu 1sem

Matrix A & B is not multiplicable 11) Design, develop and execute a parallel program in C to add, element-wise, two one-dimensional arrays A and B of N integer elements and to store the result in another one-dimensional array C of N integer elements. Algorithm 1. Start 2. Read n 3. For i 0 to n in steps of 1 Read elements of Array A END For 4. For j 0 to n in steps of 1 Read elements of Array B 5. For i0, to n in steps of 1 c[i]=a[i]+b[i] Print C 6. Stop. program #include<stdio.h> #include<omp.h> void main() { int i,j,k,n,a[10],b[10],c[10]; printf("enter the size of an arrays\n"); scanf("%d",&n); printf("enter array elements of A\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter array elements of B\n"); for(j=0;j<n;j++) scanf("%d",&b[j]); #pragma omp parallel for for(i=0;i<n;i++) { c[i]= a[i]+b[i]; printf ("c[%d]=%d,threadno=%d\n",i,c[i],omp_get_thread_num); } } Sample output enter the size of an arrays 5E nter array elements of A 24681 0 Enter array elements of B 12345T he array elements of C are C [0] =3 C [1] =6 C [2] =9 C [3] =12 C [4] =15 12) Design and develop a function rightrot(x,n) in C that returns the value of the integer x rotated to the right by n bit positions as an unsigned integer. Invoke the function from the main with different values for x and n and print the results with suitable headings. Algorithm 1. Start
http://ccp-lab-manual.blogspot.in/ 14/20

4/10/2014

ccp lab manual vtu 1sem

2. Read x and n 3. Call function RR(x, n) 6. Print the result 7. Stop Algorithm to rotate value by n bits 1. IF n==0 Return x ELSE Return ((x >> n) | (x << (32- n))) 2. Return

Program #include<stdio.h> void main ( ) { unsigned int ans,x,n; printf("enter x and n values\n"); scanf("%d%d",&x,&n); ans=rightrot(x,n); printf("\nThe value after rotating %d bit is : ",n); printf("%d",ans); } int rightrot(unsigned int x,unsigned int n) { if (n == 0) return x; else return ((x >> n) | (x << ( 32- n))); } Sample Output Enter x and n values 83 The value after rotating 3 bit is: 1 13) Design and develop a function isprime(x) that accepts an integer argument and returns 1 if the argument is prime and 0 otherwise. The function is to use plain division checking approach to determine if a given number is prime. Invoke this function from the main with different values obtained from the user and print appropriate messages. Algorithm 1. Start 2. Read the number 3. Call function prime(n) P= prime(n) 4. IF p 1 Print Number is prime ELSE Print Number not a prime End IF 5. Stop. Algorithm to check the prime number 1. FOR i 2 to n/2 in steps of 1 IF (n mod i) is equal to zero Return 0 Return 1 End For 2. Return. Program
http://ccp-lab-manual.blogspot.in/ 15/20

4/10/2014

ccp lab manual vtu 1sem

#include<stdio.h> int prime(int n) { int n,i; for(i=2;i<=n/2;i++) if(n%i ==0) return 0; return 1; } void main() { int n,p; printf("enter the no\n"); scanf("%d",&n); p=prime(n); if(p==1) printf("The given no is prime\n"); else printf("The given no is not prime\n"); } Sample output Enter the number 17 The given number is prime Enter the number 33 The given number is not prime 14) Design, develop and execute a parallel program in C to determine and print the prime numbers which are less than 100 making use of algorithm of the sieve of Eratosthenes. Algorithm 1. Start 2. Initialize n 100 3. For i0 to n in steps of 1 num[i] i END For 4. For i2 to in steps of 1 IF num[i] != 0 For j i*i to n in steps of j j+i num[j] 0 END For END IF END For 5. For i0 to n in steps of 1 iF num[i] != 0 Print non zero numbers END IF END For 6. Stop Program #include<stdio.h> #include<math.h> #include<omp.h> int main() { int num[120], i, j,n; printf("enter the value of n\n); scanf(%d,&n);
http://ccp-lab-manual.blogspot.in/ 16/20

4/10/2014

ccp lab manual vtu 1sem

#pragma omp parallel for for(i=0;i<=n;i++) { num[i]=i; } #pragma omp parallel for for(i=2;i<=sqrt(n);i++) { if(num[i]!=0) { #pragma omp parallel for for(j=(i*i);j<=n;j=j+i) { num[j]=0; }} } #pragma omp parallel for printf(The prime numbers that are less 100\n); for(i=0;i<=n;i++) { if(num[i]!=0) printf("\nprimeno=%d threadID=%d\n",num[i],omp_get_thread_num()); } } Sample output The prime numbers that are less than 100 Prime no=2 threadID=0 Prime no=3 threadID=0 Prime no=5 threadID=0 Prime no=7 threadID=0 Prime no=11 threadID=0 Prime no=13 threadID=0 Prime no=17 threadID=0 Prime no=19 threadID=0 Prime no=23 threadID=0 Prime no=29 threadID=0 Prime no=31 threadID=0 Prime no=37 threadID=0 Prime no=41 threadID=0 Prime no=43 threadID=0 Prime no=47 threadID=0 Prime no=53 threadID=0 Prime no=59 threadID=0 Prime no=61 threadID=0 Prime no=67 threadID=0 Prime no=71 threadID=0 Prime no=73 threadID=0 Prime no=79 threadID=0 Prime no=83 threadID=0 Prime no=89 threadID=0 Prime no=97 threadID=0 15) Design and develop a function reverses(s) in C to reverse the string s in place. Invoke this function from the main for different strings and print the original and reversed strings. Algorithm 1. Start 2. Read the string 3. Print the string
http://ccp-lab-manual.blogspot.in/ 17/20

4/10/2014

ccp lab manual vtu 1sem

4. Call function reverse(s) 5. Stop Algorithm to reverse a string 1. Start 2. Calculate the length 3. For i 0, i<string length/2 in step of 1 temp str[i] str[i]str[len] str[len]temp decrement length END For 4. Print the reverse string 5. Stop Flowchart

Program #include <stdio.h> #include <string.h> void reverse(char s[]) { char s[10],temp; int i,len; len = strlen(s)-1; for(i=0;i<strlen(s)/2;i++) { temp=s[i]; s[i]=s[len]; s[len]=temp; len--; } printf("the reversed string is = %s",s); } void main() { char s[10]; int i; printf("Enter String : "); scanf("%s",s); printf("the original string is=%s\n",s); reverse(s); } Sample Output Enter String : computer the original string is=computer the reversed string is=retupmoc Enter String : magic the original string is=magic the reversed string is=cigam 16) Design and develop a function matchany(s1,s2) which returns the first location in the string s1 where any character from the string s2 occurs, or -1 if s1 contains no character from s2. Do not use the standard library function which does a similar job! Invoke the function matchany(s1,s2) from the main for different strings and print both the strings and the return value from the function matchany(s1,s2). Algorithm 1. Start 2. Read string in str1 3. Read string in str2
http://ccp-lab-manual.blogspot.in/ 18/20

4/10/2014

ccp lab manual vtu 1sem

4. Call function x = matchany(str1,str2) 5. IF x is equal -1 Print no character matching ELSE Print position and matched character End if 6. Stop. Algorithm of matchany function 1.For i 0 to s1[i] != null in steps of 1 For j 0 to s2[j] != null in steps of 1 IF s1[i] = s2[j] Return i End IF End For End For 2. Return -1 3. Return Program #include <stdio.h> #include <string.h> int matchany(char s1[ ], char s2[ ]); void main() { char str1[20],str2[20]; int x; printf("\nEnter first String:"); scanf("%s",str1); printf("\nEnter second String:"); scanf("%s",str2); x=matchany(str1,str2); if(x==-1)printf("No charecter matching\n"); else printf("The position is %d and charecter is %c\n",x+1,str1[x]); } int matchany(char s1[10], char s2[10]) { int i,j; for(i=0;s1[i]!='\0';i++) { for(j=0;s2[j]!='\0';j++) { if(s1[i] == s2[j]) return i; } } return -1; } Sample Output Enter first String: india Enter second String: mandia The position is 1 and character is i Enter first String: hkbk Enter second String: class No character matching END

http://ccp-lab-manual.blogspot.in/

19/20

4/10/2014

ccp lab manual vtu 1sem

ccp program by mahesh


Posted by mahesh m at 03:36 No comments:

+2 Recommend this on Google

Home
Subscribe to: Posts (Atom)

ONEFACT07

Posts All Comments

ccp-lab-manual. Picture Window template. Powered by Blogger.

http://ccp-lab-manual.blogspot.in/

20/20

You might also like