C Record (1) (2)
C Record (1) (2)
STRINGS
1. Write a C program to concatenate two strings without built-in functions.
Source code :
#include <stdio.h>
int main()
char str1[50],str2[50];
char result[70];
int i, j;
gets(str1);
gets(str2);
result[i] = str1[i];
result[i + j] = str2[j];
result[i + j] = '\0';
return 0;
}
2
Source code :
#include<stdio.h>
int main()
{ int a=0,i;
char s1[20],s2[20];
printf("enter s1:\n");
gets(s1);
printf("enter s2:\n");
gets(s2);
for(i=0;s1[i]!='\0'||s2[i]!='\0';i++)
{ if(s1[i]!=s2[i])
{ a=1;
break;
if(a==0)
else
}
3
3. Write a C program to find the length of the string without using built-in functions.
Source code :
#include<stdio.h>
main()
int count=0,i=0;
char name[20];
printf("enter name:\n");
gets(name);
while(name[i]!='\0')
count ++;
i++;
}
4
4. Write a C program to copy one string to another string without built-in functions.
Source code :
#include<stdio.h>
main()
char str1[20],str2[20];
int i;
gets(str1);
for(i=0;str1[i]!='\0';i++)
str2[i]=str1[i];
str2[i]='\0';
getch();
}
5
5. Write a C program to find whether the string is palindrome or not without built-in funtions.
AIM : To find the whether the string is a palindrome or not without built-in functions.
Source Code :
#include <stdio.h>
int main()
{ char str[100];
int i, L, flag = 0;
scanf("%s", str);
L = 0;
{ L++;
flag = 1;
break;
} }
if (flag == 0)
else
return 0;
}
6
Source code :
#include<stdio.h>
main()
char s1[20],s2[20];
int i,l=0;
printf("enter s1:\n");
gets(s1);
while(s1[l]!='\0')
l++;
for(i=0;s1[i]!='\0';i++)
s2[i]=s1[l-i-1];
s2[i]!='\0';
}
7
AIM : To demonstrate the differences between structures and unions using a C program.
Source code:
#include<stdio.h>
union stud
{ int roll;
float marks;
char name[50];
};
struct student
{ int roll;
float marks;
char name[50];
};
main()
{ union stud X;
scanf("%d",&X.roll);
scanf("%f",&X.marks);
scanf("%s",&X.name);
printf("\n marks:%f",X.marks);
printf("\n name:%s",X.name);
struct student S;
scanf("%d",&S.roll);
scanf("%f",&S.marks);
scanf("%s",&S.name);
printf("\n marks:%f",S.marks);
printf("\n name:%s",S.name);
}
9
Source code:
#include<stdio.h>
struct complex
float real;
float img;
};
main()
scanf("%f%f",&c1.real,&c1.img);
scanf("%f%f",&c2.real,&c2.img);
print_complex(c1);
print_complex(c2);
sum_complex(c1,c2);
mul_complex(c1,c2);
return 0;
}
10
printf("%f+i%f",c3.real,c3.img);
c5.real=c3.real+c4.real;
c5.img=c3.img+c4.img;
print_complex(c5);
c6.real=c3.real*c4.real-c3.img*c4.img;
c6.img=c3.real*c4.img+c4.real*c3.img;
print_complex(c6);
}
11
Source code:
#include<stdio.h>
struct complex
float real;
float img;
};
main()
scanf("%f%f",&c1.real,&c1.img);
scanf("%f%f",&c2.real,&c2.img);
print_complex(c1);
print_complex(c2);
sum_complex(c1,c2);
mul_complex(c1,c2);
c7=mul_complex(c1,c2);
print_complex(c7);
12
printf("%f+i%f",c3.real,c3.img);
c5.real=c3.real+c4.real;
c5.img=c3.img+c4.img;
print_complex(c5);
c6.real=c3.real*c4.real-c3.img*c4.img;
c6.img=c3.real*c4.img+c4.real*c3.img;
return c6;
}
13
Functions
1.Write a C function to find the length of a string.
Source code:
#include<stdio.h>
int main()
char str[100];
int i, length=0;
printf("Enter a string:");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
length++;
return 0;
}
14
AIM: To write a program using function without return type and without parameter list.
Source code :
#include<studio>
float avg ( );
main( )
float x;
x=avg( );
printf("average=%f",x);
return 0;
float avg( )
int m1,m2,m3;
float avg ;
scanf("%d %d %d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
return avg;
AIM: To write a program using function without return type and with parameter list.
15
Source code:
#include<stdio.h>
void avg(int,int,int);
main()
int m1,m2,m3;
scanf("%d %d %d",&m1,&m2,&m3);
avg(m1,m2,m3);
float avg;
avg=(a+b+c)/3;
printf("avg=%f",avg);
AIM: To write a program using function with return type and without parameter list.
16
Source code:
#include<stdio.h>
void avg();
main()
avg();
void avg ()
int m1,m2,m3;
float avg;
scanf("%d %d %d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
printf("avg=%f",avg);
AIM: To write a program using function with return type and with parameter list.
17
Source code:
Source code:
#include<stdio.h>
float avg(int,int,int);
main()
float f;
int m1,m2,m3;
scanf("%d %d %d",&m1,&m2,&m3);
f=avg(m1,m2,m3);
printf("average=%f",f);
float x;
x=(a+b+c)/3;
return x;
Source code:
#include<stdio.h>
void sum(int,int);
main()
int a,b;
scanf("%d %d",&a,&b);
sum(a,b);
int sum=0;
sum=a+b;
printf("sum=%d",sum);
Recursion
19
Source code :
#include <stdio.h>
int fib(int n)
if (n <= 1)
return n;
else
int main()
int n;
scanf("%d", &n);
printf("\n");
return 0; }
AIM : To write a C program on recursive function to find the LCM of two numbers.
20
Source code :
#include <stdio.h>
int main()
int a, b, result;
return 0;
return common;
common++;
lcm(a, b);
Source code :
#include<stdio.h>
int rec_fact(int);
main()
int n,fact;
scanf("%d",&n);
fact=rec_fact(n);
printf("factorial of %d is %d",n,fact);
int rec_fact(int n)
if(n==0)
return 1;
else
return n*rec_fact(n-1);
Source code :
#include<stdio.h>
int sum_rec(int);
main()
int num,sum;
printf("enter a number");
scanf("%d",&num);
sum=sum_rec(num);
printf("sum=%d",sum);
if(num!=0)
return num+sum_rec(num-1);
else
return 0;
Pointers
23
Source code :
#include <stdio.h>
*num1 = *num2;
*num2 = temp;
int main()
int a, b;
swap(&a, &b);
return 0;
2. Write a C program to find no of lowercase, uppercase, digits and other characters using pointers.
24
AIM : To write a C program to find no of lowercase, uppercase, digits and other characters using
pointers.
Source code :
#include <stdio.h>
void countCharacters(const char *str, int *lowercase, int *uppercase, int *digits, int *others) {
(*lowercase)++;
(*uppercase)++;
(*digits)++;
} else {
(*others)++;
str++; }}
int main() {
char inputString[100];
return 0;
Source code :
#include <stdio.h>
int main()
printf("\n");
*(ptr + 0) = 10;
printf("\n");
return 0;
}
26
Files
1. Write a C program to write and read text into a file.
Source code :
#include <stdio.h>
int main()
FILE *filePointer;
char data[100];
if (filePointer == NULL)
return 1;
fclose(filePointer);
if (filePointer == NULL)
return 1;
{ printf("%s", data); }
fclose(filePointer); return 0; }
27
AIM : To write a C program to copy the contents of one file to another file.
Source code :
28
AIM : To write a C program to find no. of lines, words and characters in a file.
Source code :
#include <stdio.h>
int main() {
FILE *fp;
char ch;
char filename[50];
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
return 1; }
characters++;
if (ch == '\n') {
lines++; }
words++; } }
words++; }
fclose(fp);
return 0; }
29
4. Write a C program to read integer values from the user and store them in even.txt if the number is
even and in odd.txt if the number is odd.
AIM : To write a C program to read integer values from the user and store them in
Source code :