0% found this document useful (0 votes)
7 views

C Record (1) (2)

Uploaded by

alfredweizmann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C Record (1) (2)

Uploaded by

alfredweizmann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

1

STRINGS
1. Write a C program to concatenate two strings without built-in functions.

AIM : To write a 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;

printf("please enter str1:\n");

gets(str1);

printf("please enter str2:\n");

gets(str2);

for (i = 0; str1[i] != '\0'; i++)

result[i] = str1[i];

for (j = 0; str2[j] != '\0'; j++)

result[i + j] = str2[j];

result[i + j] = '\0';

printf("Concatenated string: %s\n", result);

return 0;

}
2

2. Write a C program to compare two strings without built-in functions.

AIM : To compare two strings without built-in functions

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)

printf("the two strings are same.\n");

else

printf("the two strings are not same.\n");

}
3

3. Write a C program to find the length of the string without using built-in functions.

AIM : To find the length of the string 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++;

printf("\nthe length of the string is : %d",count);

}
4

4. Write a C program to copy one string to another string without built-in functions.

AIM : To copy one sting to another string without built-in functions

Source code :

#include<stdio.h>

main()

char str1[20],str2[20];

int i;

printf("enter your name :");

gets(str1);

for(i=0;str1[i]!='\0';i++)

str2[i]=str1[i];

str2[i]='\0';

printf("copied string : %s",str2);

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;

printf("Enter a string: ");

scanf("%s", str);

L = 0;

while (str[L] != '\0')

{ L++;

for (i = 0; i < L / 2; i++)

{ if (str[i] != str[L - i - 1])

flag = 1;

break;

} }

if (flag == 0)

{ printf("%s is a palindrome.\n", str);

else

{ printf("%s is not a palindrome.\n", str);

return 0;

}
6

6. Write a C program to reverse a string without built-in functions.

AIM : To reverse a string without built-in functions.

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';

printf("the reversed string s2 is :%s",s2);

}
7

Structures and Unions


1.Write a C program to demonstrate the differences between structures and unions using a C program.

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;

printf("\n please enter roll no:");

scanf("%d",&X.roll);

printf("\n please enter marks:");

scanf("%f",&X.marks);

printf("\n enter the name:");

scanf("%s",&X.name);

printf("\n roll no:%d",X.roll);

printf("\n marks:%f",X.marks);

printf("\n name:%s",X.name);

struct student S;

printf("\n please enter roll no:");


8

scanf("%d",&S.roll);

printf("\n please enter marks:");

scanf("%f",&S.marks);

printf("\n enter the name:");

scanf("%s",&S.name);

printf("\n roll no:%d",S.roll);

printf("\n marks:%f",S.marks);

printf("\n name:%s",S.name);

}
9

2. Write a C program to illustrate the concept of passing structures to functions.

AIM: To illustrate the concept of passing structures to functions.

Source code:

#include<stdio.h>

void print_complex(struct complex);

void sum_complex(struct complex,struct complex);

void mul_complex(struct complex,struct complex);

struct complex

float real;

float img;

};

main()

struct complex c1,c2;

printf("please enter real and imaginary parts of c1 :");

scanf("%f%f",&c1.real,&c1.img);

printf("please enter real and imaginary parts of c2 :");

scanf("%f%f",&c2.real,&c2.img);

printf("\n complex number c1 is :");

print_complex(c1);

printf("\n complex number c2 is:");

print_complex(c2);

printf("\n sum of the two complex numbers is :");

sum_complex(c1,c2);

printf("\n product of the two complex numbers is :");

mul_complex(c1,c2);

return 0;

}
10

void print_complex(struct complex c3)

printf("%f+i%f",c3.real,c3.img);

void sum_complex(struct complex c3, struct complex c4)

struct complex c5;

c5.real=c3.real+c4.real;

c5.img=c3.img+c4.img;

print_complex(c5);

void mul_complex(struct complex c3,struct complex c4)

struct complex c6;

c6.real=c3.real*c4.real-c3.img*c4.img;

c6.img=c3.real*c4.img+c4.real*c3.img;

print_complex(c6);

}
11

3. Write a C program to illustrate the concept of returning structures from functions.

AIM: To illustrate the concept of returning structures from functions.

Source code:

#include<stdio.h>

void print_complex(struct complex);

void sum_complex(struct complex,struct complex);

struct complex mul_complex(struct complex,struct complex);

struct complex

float real;

float img;

};

main()

struct complex c1,c2,c7;

printf("please enter real and imaginary parts of c1 :");

scanf("%f%f",&c1.real,&c1.img);

printf("please enter real and imaginary parts of c2 :");

scanf("%f%f",&c2.real,&c2.img);

printf("\n complex number c1 is :");

print_complex(c1);

printf("\n complex number c2 is:");

print_complex(c2);

printf("\n sum of the two complex numbers is :");

sum_complex(c1,c2);

printf("\n product of the two complex numbers is :");

mul_complex(c1,c2);

c7=mul_complex(c1,c2);

print_complex(c7);
12

void print_complex(struct complex c3)

printf("%f+i%f",c3.real,c3.img);

void sum_complex(struct complex c3, struct complex c4)

struct complex c5;

c5.real=c3.real+c4.real;

c5.img=c3.img+c4.img;

print_complex(c5);

struct complex mul_complex(struct complex c3,struct complex c4)

struct complex c6;

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.

AIM: 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++;

printf("\n length of the string is:%d",length);

return 0;

}
14

2.Write a function without return type and without parameter list.

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 ;

printf("Enter three subject marks :);

scanf("%d %d %d",&m1,&m2,&m3);

avg=(m1+m2+m3)/3;

return avg;

3.Write a function without return type and with parameter list.

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;

printf("Enter three subject marks:");

scanf("%d %d %d",&m1,&m2,&m3);

avg(m1,m2,m3);

void avg (int a,int b,int c)

float avg;

avg=(a+b+c)/3;

printf("avg=%f",avg);

4.Write a function with return type and without parameter list.

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;

printf("Enter three subject marks:");

scanf("%d %d %d",&m1,&m2,&m3);

avg=(m1+m2+m3)/3;

printf("avg=%f",avg);

5.Write a function with return type and with parameter list.

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;

printf("Enter three subject marks:");

scanf("%d %d %d",&m1,&m2,&m3);

f=avg(m1,m2,m3);

printf("average=%f",f);

float avg (int a, int b, int c)

float x;

x=(a+b+c)/3;

return x;

6. Write a function to illustrate the concept of pass by value


18

Aim: To write a function to illustrate the concept of pass by value

Source code:

#include<stdio.h>

void sum(int,int);

main()

int a,b;

printf("please enter values of a and b:");

scanf("%d %d",&a,&b);

sum(a,b);

void sum (int a,int b)

int sum=0;

sum=a+b;

printf("sum=%d",sum);

Recursion
19

1. Write a recursive function to generate Fibonacci series.

AIM : To write a program on recursive function to generate Fibonacci series.

Source code :

#include <stdio.h>

int fib(int n)

if (n <= 1)

return n;

else

return fib(n - 1) + fib(n - 2);

int main()

int n;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("The first %d terms of the Fibonacci series are: ", n);

for (int i = 0; i < n; i++)

printf("%d ", fib(i));

printf("\n");

return 0; }

2. Write a recursive function to find the LCM of two numbers

AIM : To write a C program on recursive function to find the LCM of two numbers.
20

Source code :

#include <stdio.h>

int lcm(int, int);

int main()

int a, b, result;

printf("Enter two numbers: ");

scanf("%d%d", &a, &b);

result = lcm(a, b); //call the function lcm recursively.

printf("The LCM of %d and %d is %d\n", a, b, result);

return 0;

int lcm(int a, int b)

static int common = 1;

if (common % a == 0 && common % b == 0)

return common;

common++;

lcm(a, b);

3.Write a recursive function to find the factorial of a number.

AIM : To write a C program on recursive function to find the factorial of a number.


21

Source code :

#include<stdio.h>

int rec_fact(int);

main()

int n,fact;

printf("enter the value of n:\n");

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);

4. Write a recursive function to find the sum of series.

AIM : To write a C program on recursive function to find the sum of series.


22

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);

int sum_rec(int num)

if(num!=0)

return num+sum_rec(num-1);

else

return 0;

Pointers
23

1. Write a C program to swap two numbers using call by reference.

AIM : To write a C program to swap two numbers using call by reference.

Source code :

#include <stdio.h>

void swap(int *num1, int *num2)

int temp = *num1;

*num1 = *num2;

*num2 = temp;

int main()

int a, b;

printf("Enter two numbers:\n");

scanf("%d %d", &a, &b);

printf("Before swapping: a = %d, b = %d\n", a, b);

swap(&a, &b);

printf("After swapping: a = %d, b = %d\n", 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) {

while (*str != '\0')

{ if ((*str >= 'a') && (*str <= 'z')) {

(*lowercase)++;

} else if ((*str >= 'A') && (*str <= 'Z')) {

(*uppercase)++;

} else if ((*str >= '0') && (*str <= '9')) {

(*digits)++;

} else {

(*others)++;

str++; }}

int main() {

char inputString[100];

int lowercase = 0, uppercase = 0, digits = 0, others = 0;

printf("Enter a string: ");

fgets(inputString, sizeof(inputString), stdin);

countCharacters(inputString, &lowercase, &uppercase, &digits, &others);

printf("Lowercase characters: %d\n", lowercase);

printf("Uppercase characters: %d\n", uppercase);

printf("Digits: %d\n", digits);

printf("Other characters: %d\n", others);

return 0;

3. Write a C program to manipulate the array values using pointers.


25

AIM : To write a C program to manipulate the array values using pointers.

Source code :

#include <stdio.h>

int main()

int arr[] = {1, 2, 3, 4, 5};

int *ptr = arr;

printf("Original array values: ");

for (int i = 0; i < 5; i++)

printf("%d ", *(ptr + i));

printf("\n");

*(ptr + 0) = 10;

printf("Modified array values: ");

for (int i = 0; i < 5; i++)

printf("%d ", *(ptr + i));

printf("\n");

return 0;

}
26

Files
1. Write a C program to write and read text into a file.

AIM : To write a C program to write and read text into a file.

Source code :

#include <stdio.h>

int main()

FILE *filePointer;

char data[100];

filePointer = fopen("example.txt", "w");

if (filePointer == NULL)

printf("Could not open the file for writing.\n");

return 1;

printf("Enter text to write to the file: ");

fgets(data, sizeof(data), stdin);

fprintf(filePointer, "%s", data);

fclose(filePointer);

filePointer = fopen("example.txt", "r");

if (filePointer == NULL)

{ printf("Could not open the file for reading.\n");

return 1;

printf("\nReading from the file:\n");

while (fgets(data, sizeof(data), filePointer) != NULL)

{ printf("%s", data); }

fclose(filePointer); return 0; }
27

2. Write a C program to copy the contents of one file to another file.

AIM : To write a C program to copy the contents of one file to another file.

Source code :
28

3. Write a C program to find no. of lines, words and characters in a file.

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;

int lines = 0, words = 0, characters = 0;

char filename[50];

printf("Enter the filename: ");

scanf("%s", filename);

fp = fopen(filename, "r");

if (fp == NULL) {

printf("Error opening file!\n");

return 1; }

while ((ch = fgetc(fp)) != EOF) {

characters++;

if (ch == '\n') {

lines++; }

if (ch == ' ' || ch == '\t' || ch == '\n') {

words++; } }

if (characters > 0 && ch != ' ' && ch != '\t' && ch != '\n') {

words++; }

fclose(fp);

printf("Lines: %d\n", lines);

printf("Words: %d\n", words);

printf("Characters: %d\n", characters);

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

even.txt if the number is even and in odd.txt if the number is odd.

Source code :

You might also like