Sei sulla pagina 1di 51

LATHA MATHAVAN POLYTECHNIC COLLEGE, KIDARIPATTI, MADURAI

DEPARTMET OF COMPUTER ENGINEERING

PROGRAMMING LAB MANUAL (25236)


(L-SCHEME)

PREPARED BY R.SUCILA VANA ROJA, LECTURER,CT, LMPC,KIDARIPATTI, MADURAI.

CONTENTS
S.N O DATE EXP NAME PART-A
1. 2. 3. 4. 5. 6. 7. 8. 9. 1 0. SIMPLE AND COMPOUND INTEREST SWAPPING TWO VARIABLES LARGEST OF GIVEN THREE NUMBERS PALINDROME OR NOT LOWERCASE CHARACTER INTO UPPER CASE AND VICE VERSA TOTAL MARKS USING ARRAY OF STRUCTURES SUM AND AVERAGE OF GIVEN 3 NUMBERS LENGTH OF STRING USING POINTERS ADDRESS OF VARIABLE MACRO TO SWAP NUMBERS.

PAG E NO

MARKS AWARDE D

STAF F SIGN

PART-B
1 1. 1 2. 1 3. 1 4. 1 5. 1 6. 1 7. SUM OF ALL INDIVIDUAL DIGITS AND PRINT IN REVERSE PRINT THE GIVEN NUMBER INTO EQUIVALENT WORD FACTORIAL OF A GIVEN NUMBER

ARRANGE NAMES IN ALPHABETICAL ORDER

REMOVING SUBSTRINGS

READ TEN VALUES TO AN ARRAY VARIABLE

REVERSE STRING AND INTEGER IN ARRAY

1 8. 1 9. 2 0.

C PROGRAM TO PRINT THE ABBREVIATION

COPY CONTENT OF FILE TO ANOTHER

PRINTING STRING ARGUMENTS IN REVERSE

EXP NO : 1 DATE: AIM:

SIMPLE AND COMPOUND INTEREST

To write a C program to calculate simple and compound interest. REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

ALGORITHM: 1. Start the program. 2. Get the values for P, N and R from the user as input. 3. Using the formula calculate simple and compound interest. si=p*n*r/100; ci= p*pow((1+r/100),n)-p 4. Print the values for simple and compound interest. 5. Stop the program. PROGRAM #include<stdio.h> void main() { float p,n,r,si,ci; clrscr(); printf("Enter the values for P,N,R"); scanf("%f%f%f",&p,&n,&r); si=p*n*r/100; ci=p*pow((1+r/100),n)-p; printf("Simple Interest = %f",si); printf("\nCompound Interest = %f",ci);

getch(); }

OUTPUT Enter the values for P,N,R 1000 5 3 Simple Interest = 150.000000 Compound Interest = 254000.000000

RESULT: Thus, the program to calculate simple and compound interest was written, compiled, executed and output was verified. EXP NO : 2(i) DATE: AIM: To write a C program to calculate swap two numbers i) using third variable ii) without using third variable REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

SWAPPING TWO VARIABLES

ALGORITHM: (using third variable) 1. Start the program. 2. get two values x and y from user as input. 3. print the values before swapping 4.swap the values by using third variable. 5. Stop the program. ALGORITHM: (without using third variable) 1. Start the program. 2. get two values x and y from user as input. 3. print the values before swapping 4.swap the values by using third variable. 5. Stop the program. PROGRAM(using third variable)

#include <stdio.h> int main() { int x, y, temp; clrscr(); printf("Enter the value of x and y\n"); scanf("%d%d", &x, &y); printf("Before Swapping\nx = %d\ny = %d\n",x,y); temp = x; x = y; y = temp; printf("After Swapping\nx = %d\ny = %d\n",x,y); getch(); return 0; }

OUTPUT Enter the value of x and y 12 67 Before Swapping x = 12 y = 67 After Swapping x = 67 y = 12 PROGRAM(without using third variable) #include <stdio.h> int main() { int a, b; clrscr();

printf("Enter two integers to swap\n"); scanf("%d%d", &a, &b); printf("Before Swapping\na = %d\nb = %d\n",a,b); a = a + b; b = a - b; a = a - b; printf("After swapping \na = %d\nb = %d\n",a,b); getch(); return 0; }

OUTPUT Enter two integers to swap 34 90 Before Swapping a = 34 b = 90 After swapping a = 90 b = 34

RESULT Thus a c program to swap two values i) using third variable and ii) without third variable was written, compiled, executed and output was verified. EXP NO : 3 DATE: LARGEST OF GIVEN THREE NUMBERS

AIM: To write a c program to find the largest of the given three numbers. REQUIREMENT: ALGORITHM: 1. Start the program. 2. Get three values a,b,c from user as input. 3. Using if statement check whether a is greater than b if true then check whether a is greater than c. 4. If true,print a is largest.else print c is largest. 5. If the if statement becomes false,then check whether b is greater than c. 6. If the condition is true print b is largest. 7. Stop the program. PROGRAM #include<stdio.h> #include<conio.h> Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

void main() { int a,b,c; clrscr(); printf("\n\nEnter The value of a,b,c\n\n"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) printf("largest=%d",a); else printf("largest=%d",c); } else { if (b>c) printf("largest=%d",b); else printf("largest=%d",c); } getch(); } OUTPUT Enter The value of a,b,c 12 34 89 largest=89

RESULT Thus a c program to find the largest of three numbers was written, compiled, executed and output was verified. EXP NO : 4 DATE: PALINDROME

AIM: To write a c program to find the largest of the given three numbers. REQUIREMENT: ALGORITHM: 1. 2. 3. 4. 5. 6. 7. 8. PROGRAM Start the program. Get a string from user as input and store it in array a. Copy srting in array a to array b. Now reverse the string in b. Using strcmp() function compare two strings If they are equal print the string is palindrome Otherwise print the string is not palindrome. Stop the program. Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

#include<stdio.h> main() { char a[20],b[20]; clrscr(); printf("enter a string"); scanf("%s",&a); strcpy(b,a);//copies string a to b strrev(b);//reverses string b if(strcmp(a,b)==0)//compares if the original and reverse strings are same printf("\n%s is a palindrome",a); else printf("\n%s is not a palindrome",a); getch(); return 0; }

OUTPUT enter a string aruna aruna is not a palindrome enter a string madam madam is a palindrome

RESULT Thus a c program to check whether the given string is palindrome or not was written, compiled, executed and output was verified. EXP NO : 5 DATE: CONVERT LOWERCASE CHARACTER INTO UPPER CASE AND VICE VERSA

AIM To write a c program to convert the string in uppercase to lowercase and vice versa. REQUIREMENT: ALGORITHM 1. 2. 3. 4. Start the program. Get a string from user as input using gets() function. Using strupr() function convert the string into uppercase. Using strlwr()function convert the string into lowercase. Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

5. Print the string. 6. Stop the program. PROGRAM #include<stdio.h> #include<string.h> #include<conio.h> void main() { char s1[20]; clrscr(); printf(\n enter a string with upper and lowercase letters\n); gets(s1); printf(The string after converting to uppercase\t%s\n,strupr(s1)); printf(The string after converting to lowercase\t%s\n,strlwr(s1)); getch(); }

OUTPUT enter a string with upper and lowercase letters esther The string after converting to uppercase The string after converting to lowercase ESTHER esther

RESULT Thus a c program to convert the given string from uppercase to lowercase and vice versa was written, compiled, executed and output was verified. EXP NO : 6 DATE: TOTAL MARKS USING ARRAY OF STRUCTURES

AIM To write a c program to calculate total marks using array of structures. REQUIREMENT: ALGORITHM 1. Start the program. Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

2. 3. 4. 5. 6. 7. 8.

Declare a structure student with variables name, roll no, mark, tot. Declare the necessary variables. Create a structure variable using arrays. Get the student roll no, name, mark using for loop. Calculate the total marks using arithmetic operator. Using for loop display name, roll no and total for each student. Stop the program.

PROGRAM #include<stdio.h> #include<conio.h> struct student { int rollno,tot; char name[25]; int mark[5]; }; void main() { struct student s[5]; int i,n,j; clrscr(); printf("Enter the number of students:"); scanf("%d",&n); printf("\t*Students Records*\n"); //take input from user for(i=0;i<n;i++) { printf("\nEnter Student Roll Number: "); scanf("%d",&s[i].rollno); printf("\nEnter Student name: "); scanf("%s",s[i].name); printf("\nEnter Student 3 subject's marks: "); for(j=0;j<3;j++) scanf("%d",&s[i].mark[j]); //Data type of '*s' is struct student

} //calculation for(i=0;i<n;i++) { s[i].tot=0; for(j=0;j<3;j++) s[i].tot = s[i].tot+ s[i].mark[j]; } //Display result for(i=0;i<n;i++) { printf("\t*Students Records*\n"); printf("\n==================================\n"); printf("\nStudent's Roll no. - %d", s[i].rollno); printf("\nStudent's Name - %s", s[i].name); printf("\nStudent's Total Marks - %d", s[i].tot); } getch(); }

OUTPUT Enter the number of students:2 *Students Records* Enter Student Roll Number: 01 Enter Student name: rathi Enter Student 3 subject's marks:

12 67 89 Enter Student Roll Number: 02 Enter Student name: raghu Enter Student 3 subject's marks: 56 89 90 *Students Records* ================================== Student's Roll no. - 1 Student's Name - rathi Student's Total Marks - 168 Student's Roll no. - 2 Student's Name - raghu Student's Total Marks - 235 *Students Records* ==================================

RESULT Thus a c program to display student total marks was written, compiled, executed and output was verified. EXP NO : 7 DATE: AIM: To write a c program to calculate sum and average of given three numbers. REQUIREMENT: Computer with PENTIUM IV/ DUAL CORE PROCESSORS SUM AND AVERAGE OF GIVEN 3 NUMBERS

ALGORITHM 1. 2. 3. 4. 5. 6. 7. 8.

TURBO C

Start the program. Declare the necessary variables. Get three values from user as input. Declare a function named sum. Define the function outside the main () function. Calculate sum and average. Print the sum and average of three numbers. Stop the program.

PROGRAM #include<stdio.h> #include<conio.h> void main() { void sum(int,int,int); int x,y,z; clrcsr(); printf(Enter the values of x,y and z); scanf(%d%d%d,&x,&y,&z); sum(x,y,z); getch(); } void sum(int a,int b,int c) { int sum,avg; sum=a+b+c; avg=sum/3; printf("\nsum of three numbers is:%d",sum); printf("\nAverageof three numbers is:%d",avg); getch(); } OUTPUT Enter the values of x,y and z

3 4 5 sum of three numbers is:12 Average of three numbers is:4

RESULT Thus a c program to calculate sum and average of three numbers was written, compiled, executed and output was verified.

EXP NO : 8 DATE: AIM

LENGTH OF STRING USING POINTERS

To write a c program to find the length of the string using pointers. REQUIREMENT:

ALGORITHM 1. 2. 3. 4. 5. 6.

Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

Start the program. Declare the needed pointer variables. Get the string from user using gets() function. using while find length by incrementing pointer variables. Finally print the length of the string. Stop the program.

PROGRAM #include<stdio.h> #include<conio.h> void main() { clrscr(); int l=0; char *p; char name[20]; p=name; clrscr(); printf(\n enter a string\n); gets(name); while(*p !=\0) { l++; p++; } printf(\n); printf(the length of the string is %d\t,l);

getch(); } OUTPUT enter a string dheena the length of the string is 6

RESULT Thus a c program to find the length of the string using pointers was written, compiled, executed and output was verified. EXP NO : 9 DATE: ADDRESS OF VARIABLE

AIM

To write a c program to find the address of a variable and increase the content by 5 and print the new value. REQUIREMENT: ALGORITHM 1. 2. 3. 4. 5. 6. Start the program. Declare the needed pointer variables. Get the string from user and print it. Increment the value by five using pointers and print after incrementing. Finally print the address of the variable. Stop the program. Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

PROGRAM #include<stdio.h> #include<conio.h> void main() { int a; int *p; clrscr(); p=&a; printf(\n enter the value for a \n); scanf(%d,&a); printf(value of a before adding 5 is %d\t,*p); printf(\n); *p=*p+5; printf(\n value of a after adding 5 is %d \t,*p); printf(\n); printf(\n the address of variable a is %d \t,p);

getch(); } OUTPUT enter the value for a 20 value of a before adding 5 is 20 value of a after adding 5 is 25 the address of variable a is -12

RESULT Thus a c program to find the address of a variable and increase the content by 5 and print the new value was written, compiled, executed and output was verified. EXP NO : 10 MACRO TO SWAP NUMBERS.

DATE:

AIM To write a macro to swap two data values. REQUIREMENT: ALGORITHM 1. 2. 3. 4. 5. 6. 7. 8. Start the program. Write a macro named swap. The macro performs the function of swapping. Get two values from user as input. Print the values before swapping. Using the macro swap two values. Print the values after swapping. Stop the program. Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

PROGRAM #include<stdio.h> #include<conio.h> #define swap(x,y) int t; t=x;x=y;y=t; void main() { clrscr(); int a,b; printf(\n enter the values for a and b\n); scanf(%d%d,&a,&b); printf(value of a and b before swap is %d\t%d,a,b); swap(a,b); printf(\n); printf(\n value of a and b after swap is %d \t %d,a,b);

getch(); }

OUTPUT enter the values for a and b 12 45 value of a and b before swap is 12 45

of a and b after swap is 45 12

RESULT Thus a c program to swap two data values was written, compiled, executed and output was verified.

EXP NO : 11 DATE:

SUM OF ALL INDIVIDUAL DIGITS AND PRINT IN REVERSE

AIM: To write a C program to calculate Sum of all individual digits and also print the above number in reverse order. REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

ALGORITHM: 1. Start the program. 2. Declare the variables count, sum, reverse. 3. Initialize the count and sum to zero. 4. Get an integer number from user as input. 5. Assign m with n. 6. Using while statement, find the sum of digits and number of digits using divide and modulo operation. 7. Print the sum of digits and number of digits of the given number. 8. Print the numbers in reverse order. 9. Stop the program. PROGRAM #include<stdio.h> #include<conio.h> void main() { int count=0,sum=0,rev; int r,n,m; clrscr(); printf("Enter an integer number"); scanf("%d",&n); m=n; printf("\n Reverse order of %d is",n);

while(m!=0) { count=count+1; r=m%10; printf("%d",r); sum=sum+r; m=m/10; } printf("\n Sum of digit for %d is %d",n,sum); printf("\n"); printf("\n Number of digits in %d is %d",n,count); getch(); } OUTPUT Enter an integer number 890 Reverse order of 890 is098 Sum of digit for 890 is 17 Number of digits in 890 is 3

RESULT:

Thus, the program to calculate Sum of all individual digits and also print the above number in reverse order was written, compiled, executed and output was verified.

EXP NO : 12 DATE:

PRINT THE GIVEN NUMBER INTO EQUIVALENT WORD

AIM: To write a C program to print the given number into equivalent Word using switch statement. REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

ALGORITHM: 1. Start the program. 2. Declare variable n. 3. Get the value of n from user as input. 4. Using switch statement convert the given numbers into equivalent word. 5. Stop the program. PROGRAM #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf(Enter an integer number between 1 to 6 \n); scanf(%d,&n); switch(n)

{ case 1: printf(ONE\n); break; case 2: printf(TWO\n); break; case 3: printf(THREE\n); break; case 4: printf(FOUR\n); break; case 5: printf(FIVE\n); break; case 6: printf(SIX\n); break; } getch(); }

OUTPUT Enter an integer number between 1 to 6 4 FOUR Enter an integer number between 1 to 6 6 SIX

RESULT: Thus, the program to print the given number into equivalent Word using switch statement was written, compiled, executed and output was verified.

EXP NO : 13 DATE:

FACTORIAL OF A GIVEN NUMBER

AIM: To write a C program to find the factorial of a given number

(i) Without recursion (ii) With recursion . REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

ALGORITHM(with recursion) 1. Start the program. 2. Declare the necessary variables. 3. Get the number from user to find the factorial. 4. Write a user defined function for factorial using the formula n*fact(n-1). 5. Print the factorial of the given number. 6. Stop the program. 7. ALGORITHM(without recursion) 1. Start the program. 2. Declare the necessary variables. 3. Get the number from user to find the factorial. 4. Using for loop find the factorial of the given number. 5. Print the factorial of the given number. 6. Stop the program. PROGRAM (i) with Recursion #include<stdio.h> #include<conio.h> void main() { int fact(int); int n,f,choice; clrscr();

printf(Enter the number to find the factorial\n); scanf(%d,&n); f=fact(n); printf(The factorial of a number %d using recursion is %d,n,f); getch(); } int fact(int n) { int f; if(n==0) return(1); else f=n*fact(n-1); return(f); }

OUTPUT Enter the number to find the factorial 5 The factorial of a number 5 using recursion is 120

(ii) Without recursion

#include<stdio.h> #include<conio.h> void main() { int fact(int); int i,n,f,choice; clrscr(); printf("Enter the number to find the factorial\n"); scanf("%d",&n); f=1; for(i=1;i<=n;i++) f=f*i; printf("The factorial of a number %d without using recursion is %d",n,f); getch(); }

OUTPUT Enter the number to find the factorial 4 The factorial of a number 4 without using recursion is 24

RESULT: Thus, the program to calculate the factorial of a given number (i) with recursion (ii) without recursion was written, compiled, executed and output was verified.
EXP NO : 14 DATE:

ARRANGE NAMES IN ALPHABETICAL ORDER

AIM: To write a C program to arrange the given N names in alphatical order.

REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS ALGORITHM: TURBO C

1. 2. 3. 4. 5. 6. 7. 8.

Start the program. Declare the needed variables. Get the number of strings from user. Get the strings from user as input. Using strcmp() function compare the first two strings. Swap the greater with the smaller and continue for next strings. Print the arranged strings. Stop the program.

PROGRAM #include<string.h> #include<conio.h> void main() { char name[20][30],temp[30]; int n,v,i,j; clrscr(); printf("Give the number of names to sort\n"); scanf("%d",&n); printf("Give the names one by one\n"); for(i=0;i<=n;i++) gets(name[i]); for(i=0;i<n;i++) for(j=i+1;j<=n;j++) { v=strcmp(name[i],name[j]); if(v>0) { strcpy(temp,name[i]); strcpy(temp,name[i]); strcpy(name[i],name[j]); strcpy(name[j], temp); } } printf("The sorted names are \n");

for(i=0;i<=n;i++) puts(name[i]); getch(); }

OUTPUT Give the number of names to sort 5 Give the names one by one zenith bragadesh seenivasan jamal yasin The sorted names are bragadesh jamal seenivasan yasin zenith

RESULT: Thus, the program to arrange the given N names in alphatical order was written , compiled, executed and output was verified.
EXP NO : 15 DATE:

REMOVING SUBSTRINGS

AIM: To write a C program to remove the substring from the given string.

REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS

TURBO C

ALGORITHM: 1. Start the program. 2. Declare the needed variables. 3. Get the string from user. 4. Get the substring from user to remove from the given string. 5. Remove the substring from the given string. 6. Finally print the remaining string. 7. Stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<string.h> int i,j,k,l,remove_string_len=0; void main() { char full_string[30],remove_string[30],result_string[30]; clrscr(); printf("\nEnter the string\n"); gets(full_string); printf("\nInput string -\"%s\"\n",full_string); printf("\nEnter the string to remove "); gets(remove_string); remove_string_len=strlen(remove_string); while(full_string[i]!=remove_string[j]) { result_string[l++]=full_string[i]; i++; }

for(k=0;k<=remove_string_len;k++) { if(full_string[i]==remove_string[k]) i++; } while(full_string[i]!='\0') { result_string[l++]=full_string[i]; i++; } result_string[l++]='\0'; printf("\nResult string after removal of substring \"%s\"",result_string); getch(); } OUTPUT: Enter the string ramkumar Input string -"ramkumar" Enter the string to remove ram Result string after removal of substring "kumar" RESULT: Thus, the program to remove a substring form the given string was written , compiled, executed and output was verified

EXP NO : 16 DATE:

READ TEN VALUES OF ARRAY VARIABLE

AIM:

To write a C program to read ten values to an array variable and to locate and display value using pointers.

REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

ALGORITHM: 1. Start the program. 2. Declare an integer array and a pointer variable. 3. Initialize the pointer variable with the address of first variable in array. 4. Get ten values from user as input. 5. Using for loop get the values from user. 6. Using pointer variable locate,display the values stored in array with for loop. 7. Stop the program. PROGRAM #include<stdio.h> #include<conio.h> void main() { int a[10]; int *p; int i; clrscr(); p=&a[0]; printf("Enter 10 integer values on by one \n"); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("The output is \n"); for(i=0;i<10;i++) { printf("%d\n",*p);

p++; } getch(); } OUTPUT: Enter 10 integer values on by one 5 8 45 78 23 90 55 77 33 65 The output is 5 8 45 78 23 90 55 77 33 65 RESULT: Thus, the program to read ten values to an array variable and to locate and display value using pointers was written , compiled, executed and output was verified.

EXP NO : 17 DATE:

REVERSE STRING AND INTEGER IN ARRAY

AIM: To write a C program to Reverse (i) String (ii) N integer numbers stored in any array using pointers.

REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

ALGORITHM:( reversing strings) 1. Start the program. 2. Declare an array, pointer variable. 3. Get the string from user using gets() function. 4. Using strlen() function find the length of the string. 5. Initialize the pointer variable with the address of last variable in array. 6. Using for loop, reverse the given string by decrementing the pointer variable. 7. Stop the program. ALGORITHM:( reversing integers) 1. Start the program. 2. Declare an array, pointer variable. 3. Get the string from user using scanf() function. 4. Initialize the pointer variable with the address of last variable in array. 5. Using for loop, reverse the given integers by decrementing the pointer variable. 6. Stop the program.

PROGRAM

(i) Reverse String #include<stdio.h> #include<conio.h> #include<string.h>

void main() { char a[20]; char *p,l; int i; clrscr(); printf("Give a string\n"); gets(a); l=strlen(a); p=&a[l]; for(i=0;i<=l;i++) { printf("%c",*p); p--; } getch(); } OUTPUT Give a string rajeev veejar

(ii) Reversing integer array #include<stdio.h> #include<conio.h> void main() { int a[20]; int *p,n,i; clrscr(); printf("Give the maximum number of integers to store\n"); scanf("%d",&n); printf("Give the integer numbers one by one\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); p=&a[n-1]; printf("The output numbers are \n"); for(i=0;i<n;i++) { printf("%d\t",*p); p--; } getch(); }

OUTPUT Give the maximum number of integers to store 4 Give the integer numbers one by one 67 90 89 76 The output numbers are 76 89 90 67

RESULT: Thus, the program to Reverse (i) String (ii) N integer numbers stored in any array using pointers was written, compiled, executed and output was verified.

EXP NO : 18 DATE:

C PROGRAM TO PRINT THE ABBREVIATION

AIM:

To write a C program to print the abbreviation of an Organization Name.

REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

ALGORITHM: 1. Start the program. 2. Declare the needed variables. 3. Get the full form of the name of the organization using gets() function. 4. With while statement, Store the first character of every string using an array. 5. Print the abbreviation of the given organization. 6. Stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<string.h> int i,j,first=0; void main() { char name[30],abbrev[10]; clrscr(); printf("Enter the full form of an organization\n"); gets(name); again: while(name[i]!=' ') { if(first==0) {

abbrev[j++]=name[i]; } first++; i++; if(i==strlen(name)-1) { goto end; } } if(name[i]==' ') { first=0; i++; goto again; } end: abbrev[j++]='\0'; printf("\n The abbreviation of %s is %s",name,abbrev); getch(); } OUTPUT Enter the full form of an organization hindustan machines The abbreviation of hindustan machines is "HM" RESULT: Thus, the program to print the abbreviation of an Organization Name was written, compiled, executed and output was verified

EXP NO : 19 DATE:

COPY CONTENT OF FILE TO ANOTHER

AIM: To write a C program to copy one file into another. . REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

ALGORITHM: 1. Start the program. 2. Create two file pointers. 3. Create two text files named as input and output. 4. The input file acts as a source file where the data to be copied is stored. 5. The output file is the destination file for storing the copied data. 6. Get the data from source file (opened in read mode)using gets function. 7. copy the data to the destination file. 8. If copy is successful the message COMPLETED is displayed. 9. Stop the program. PROGRAM #include<stdio.h> int main(){ FILE *p,*q; char file1[20],file2[20]; char ch; printf("\nEnter the source file name to be copied:"); gets(file1); p=fopen(file1,"r"); if(p==NULL){ printf("cannot open %s",file1); exit(0); } printf("\nEnter the destination file name:");

gets(file2); q=fopen(file2,"w"); if(q==NULL){ printf("cannot open %s",file2); exit(0); } while((ch=getc(p))!=EOF) putc(ch,q); printf("\nCOMPLETED"); fclose(p); fclose(q); return 0; }

OUTPUT Enter the source file name to be copied:input.txt Enter the destination file name:output.txt COMPLETED

Input.txt: Good morning Output.txt: (after copying) Good Morning

RESULT: Thus, the program to copy one file into another was written, compiled, executed and output was verified
EXP NO : 20 DATE:

PRINTING STRING ARGUMENTS IN REVERSE

AIM: To write a C program to print the string arguments in reverse order using command line arguments. . REQUIREMENTS: Computer with PENTIUM IV/ DUAL CORE PROCESSORS TURBO C

ALGORITHM: 1. Start the program. 2. Declare an array to store the strings. 3. Give the input using command line arguments. 4. Using for loop reverse the string using strcat function. 5. Get the length of the string using strlen () function. 6. Display the string in reverse using for loop. 7. Stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<string.h> void main(int argc, char*argv[]) { clrscr(); char str[50]; for(int i=1;i<=argc;i++)

{ strcat(str,argv[i]); } int l=strlen(str); for(i=l-1;i>=0;i--) printf(%c,str[i]); getch(); }

OUTPUT D:\ tc>reverse tamil nadu udanlimat

RESULT: Thus, the program to print the string arguments in reverse using command line arguments was written, compiled, executed and output was verified.

Potrebbero piacerti anche