I. SWAPPING OF TWO NUMBERS USING FUNCTION:
Program:
#include <stdio.h>
void swap(int, int);
int main()
{
int x, y;
x = 10;
y = 20;
swap(x, y);
printf("Value of x and y after swap(x, y) in main() : %d %d \n", x, y);
}
void swap(int a, int b)
{
int t;
printf("Value of a and b before exchange in swap() : %d %d \n", a, b);
t = a;
a = b;
b = t;
printf("Value of a and b after exchange in swap() : %d %d \n", a, b);
void swap(int, int);
int main()
{
int x, y;
x = 10;
y = 20;
swap(x, y);
printf("Value of x and y after swap(x, y) in main() : %d %d \n", x, y);
}
void swap(int a, int b)
{
int t;
printf("Value of a and b before exchange in swap() : %d %d \n", a, b);
t = a;
a = b;
b = t;
printf("Value of a and b after exchange in swap() : %d %d \n", a, b);
return 0;
}
}
Output:
Value of a and b before exchange in swap() : 10 20
Value of a and b after exchange in swap() : 20 10
Value of x and y after swap(x, y) in main() : 10 20
Value of a and b after exchange in swap() : 20 10
Value of x and y after swap(x, y) in main() : 10 20
II. ARRAY USING POINTERS:
Program:
#include <stdio.h>
int main()
{
int i, n;
printf("Enter the size of the array : ");
scanf("%d", &n);
int *a[n];
printf("Enter the elements : \n");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("\nThe given elements are \n");
for(i = 0; i < n; i++)
{
printf("a[%d] = %d\n", i , *(a+i));
}
return 0;
}
int main()
{
int i, n;
printf("Enter the size of the array : ");
scanf("%d", &n);
int *a[n];
printf("Enter the elements : \n");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("\nThe given elements are \n");
for(i = 0; i < n; i++)
{
printf("a[%d] = %d\n", i , *(a+i));
}
return 0;
}
Output:
Enter the size of the array : 2
Enter the elements :
10
15
The given elements are
a[0] = 10
a[1] = 15
Enter the elements :
10
15
The given elements are
a[0] = 10
a[1] = 15
III. LENGTH OF THE STRING:
Program:
#include <stdio.h>
#include <string.h>
int l(char *);
int main()
{
int len;
static char s1[] = "anand";
len = l(s1);
printf("Length of the string s1 : %d \n", len);
#include <string.h>
int l(char *);
int main()
{
int len;
static char s1[] = "anand";
len = l(s1);
printf("Length of the string s1 : %d \n", len);
return 0;
}
int l(char *ptr)
{
int s = 0;
while(*ptr != '\0')
{
s++;
ptr++;
}
return s;
}
}
int l(char *ptr)
{
int s = 0;
while(*ptr != '\0')
{
s++;
ptr++;
}
return s;
}
Output:
Length of the string s1 : 5
IV. STRING CONCATENATION:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20
int main()
{
char *s1, *s2, *s3, c;
int i = 0, k = 0;
s1 = malloc(LENGTH * sizeof(char));
s2 = malloc(LENGTH * sizeof(char));
s3 = malloc(2 * LENGTH * sizeof(char));
printf("Enter the string s1 : ");
scanf("%s", s1);
printf("Enter the string s2 : ");
scanf("%s", s2);
i = strlen(s1);
s3 = s1;
while((c = *(s2 + k)) != '\0')
{
s3[i + k] = c;
k++;
}
s3[i + k] = '\0';
printf("The concatenated string : %s \n", s3);
return 0;
}
#include <stdlib.h>
#include <string.h>
#define LENGTH 20
int main()
{
char *s1, *s2, *s3, c;
int i = 0, k = 0;
s1 = malloc(LENGTH * sizeof(char));
s2 = malloc(LENGTH * sizeof(char));
s3 = malloc(2 * LENGTH * sizeof(char));
printf("Enter the string s1 : ");
scanf("%s", s1);
printf("Enter the string s2 : ");
scanf("%s", s2);
i = strlen(s1);
s3 = s1;
while((c = *(s2 + k)) != '\0')
{
s3[i + k] = c;
k++;
}
s3[i + k] = '\0';
printf("The concatenated string : %s \n", s3);
return 0;
}
Output:
Enter the string s1 : tamil
Enter the string s2 : nadu
The concatenated string : tamilnadu
Enter the string s2 : nadu
The concatenated string : tamilnadu
V. FILE HANDLING:
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1, *fp2;
int c;
char fname1[40], fname2[40];
printf("##### Copy the data of one file to another #####\n");
printf("Enter the source file : ");
gets(fname1);
printf("Enter the target file : ");
gets(fname2);
fp1 = fopen(fname1, "r");
fp2 = fopen(fname2, "w");
if(fp1 == NULL)
{
printf("Error opening file %s for reading !\n", fname1);
exit(1);
}
else if(fp2 == NULL)
{
printf("Error opening file %s for writing ! \n", fname2);
exit(1);
}
else
{
c = getc(fp1);
while(c != EOF)
{
putc(c, fp2);
c = getc(fp1);
}
fclose(fp1);
fclose(fp2);
printf("File copied successfully...\n");
}
return 0;
}
#include <stdlib.h>
int main()
{
FILE *fp1, *fp2;
int c;
char fname1[40], fname2[40];
printf("##### Copy the data of one file to another #####\n");
printf("Enter the source file : ");
gets(fname1);
printf("Enter the target file : ");
gets(fname2);
fp1 = fopen(fname1, "r");
fp2 = fopen(fname2, "w");
if(fp1 == NULL)
{
printf("Error opening file %s for reading !\n", fname1);
exit(1);
}
else if(fp2 == NULL)
{
printf("Error opening file %s for writing ! \n", fname2);
exit(1);
}
else
{
c = getc(fp1);
while(c != EOF)
{
putc(c, fp2);
c = getc(fp1);
}
fclose(fp1);
fclose(fp2);
printf("File copied successfully...\n");
}
return 0;
}
Output:
##### Copy the data of one file to another #####
Enter the source file : file1.txt
Enter the target file : file2.txt
File copied successfully...
Enter the source file : file1.txt
Enter the target file : file2.txt
File copied successfully...
No comments:
Post a Comment