Professional Documents
Culture Documents
#include <stdio.h>
#include <string.h>
int main(void)
{
/* Compare two strings as lowercase */
if (0 == stricmp("hello", "HELLO"))
printf("The strings are equivalent.\n");
else
printf("The strings are not equivalent.\n");
return 0;
}
2. itoa
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a=54325;
char buffer[20];
itoa(a,buffer,2); // here 2 means binary
printf("Binary value = %s\n", buffer);
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "MODIFY This String To LOwer";
printf("%s\n",strlwr (str));
return 0;
}
4. strupr
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "Modify This String To Upper";
printf("%s\n",strupr(str));
return 0;
}
5. strrev
#include<stdio.h>
#include<string.h>
int main()
{
char name[30] = "Hello";
return 0;
}
6. ftoa
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char res[20];
float n = 233.007;
ftoa(n, res, 4);
printf("\n\"%s\"\n", res);
return 0;
}
#include<stdio.h>
#include<string.h>
int main() {
char str[100], temp;
int i, j = 0;
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
2. String copy
3. #include <stdio.h>
4. int main()
5. {
6. char s1[100], s2[100], i;
7.
8. printf("Enter string s1: ");
9. scanf("%s",s1);
10.
11. for(i = 0; s1[i] != '\0'; ++i)
12. {
13. s2[i] = s1[i];
14. }
15.
16. s2[i] = '\0';
17. printf("String s2: %s", s2);
18.
19. return 0;
20.}
3. String compare
#include<stdio.h>
int main() {
char str1[30], str2[30];
int i;
i = 0;
while (str1[i] == str2[i] && str1[i] != '\0')
i++;
if (str1[i] > str2[i])
printf("str1 > str2");
else if (str1[i] < str2[i])
printf("str1 < str2");
else
printf("str1 = str2");
return (0);
}
4. String concatenation
#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;
printf("Enter first string: ");
scanf("%s", s1);
s1[i] = '\0';
printf("After concatenation: %s", s1);
return 0;
}
int main()
{
char str[100];
stringLwr(str);
printf("String after stringLwr : %s\n",str);
stringUpr(str);
printf("String after stringUpr : %s\n",str);
return 0;
}
printf("Enter 3 Nos.");
scanf("%d%d%d",&m1,&m2,&m3);
p=&m1;p1=&m2;p2=&m3;
Total=*p+*p1+*p2;
per=total*100/300;
printf(\npercentage marks %f,per
getch();
}
int main()
{
int a=54325;
char buffer[20];
itoa(a,buffer,2); // here 2 means binary
printf("Binary value = %s\n", buffer);
int main()
{
char res[20];
float n = 233.007;
ftoa(n, res, 4);
printf("\n\"%s\"\n", res);
return 0;
}
atoi
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char str[]="12";
int val;
printf("%s",str);
val=atoi(str);
printf("%d",val);
getch();
}
atof
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
char str[]="12.5";
float val;
printf("%s",str);
val="12.5";
val=atof(str);
printf("%f",val);
getch();
}
Ans-
. itoa . ftoa
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int main()
{
char res[20];
float n = 233.007;
ftoa(n, res, 4);
printf("\n\"%s\"\n", res);
return 0;
}
#include <stdio.h>
int main()
{
int num1,num2;
return 0;
}
#include <stdio.h>
int main()
{
const int a=10; //declare and assign constant integer
int *p; //declare integer pointer
p=&a; //assign address into pointer p
printf("Before changing - value of a: %d",a);
return 0;
}
Q: 3 C program to print a string character by character using pointer
/*C program to print a string using pointer.*/
#include <stdio.h>
int main()
{
char str[100];
char *ptr;
return 0;
}
cntV=cntC=0;
while(*ptr!='\0')
{
if(*ptr=='A' ||*ptr=='E' ||*ptr=='I' ||*ptr=='O' ||*ptr=='U' ||*ptr=='a'
||*ptr=='e' ||*ptr=='i' ||*ptr=='o' ||*ptr=='u')
cntV++;
else
cntC++;
//increase the pointer, to point next character
ptr++;
}
void main() {
int int_var = 10, *int_ptr;
char char_var = 'A', *char_ptr;
float float_val = 4.65, *float_ptr;
/* Initialize pointers */
int_ptr = &int_var;
char_ptr = &char_var;
float_ptr = &float_val;
/* Incrementing pointers */
int_ptr++;
char_ptr++;
float_ptr++;
printf("After increment address in int_ptr = %u\n", int_ptr);
printf("After increment address in char_ptr = %u\n", char_ptr);
printf("After increment address in float_ptr = %u\n\n", float_ptr);
/* Adding 2 to pointers */
int_ptr = int_ptr + 2;
char_ptr = char_ptr + 2;
float_ptr = float_ptr + 2;
getch();
return 0;
}
/*Array declaration*/
int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ;
ptr = numArray;
p=a;
for(i=0;i<5;i++)
{
printf("\nENTER NUMBER %d: ",i+1);
scanf("%d",(p+i));
}
h=*p;
for(i=1;i<5;i++)
{
if(*(p+i)>h)
h=*(p+i);
}
printf("\nTHE HIGHEST NUMBER IS %d",h);
getch();
}
Output:
Enter number : 8
Enter number : 1
Enter number : 4
Enter number : 6
Enter number : 10
The highest number is: 10
int main () {
return 0;
}
Output:
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
array of pointers to character to store a list of strings
#include <stdio.h>
int main () {
char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali"
};
int i = 0;
return 0;
}
Output:
Value of names[0] = Zara Ali
Value of names[1] = Hina Ali
Value of names[2] = Nuha Ali
Value of names[3] = Sara Ali
Array of pointer
#include <stdio.h>
#include <stdio.h>
int main()
{
char *fruit[] = {
"watermelon",
"banana",
"pear",
"apple",
"coconut",
"grape",
"blueberry"
};
int x;
for(x=0;x<7;x++)
puts(fruit[x]);
return(0);
}
Output:
watermelon
banana
pear
apple
coconut
grape
blueberry
Pointer to array
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p = &a[0]
for (i=0; i<5; i++)
{
printf("%d", *p);
p++;
}
}
Output:
12345
printf("Displaying Information:\n");
printf("Name: ");
puts(s.name);
return 0;
}
int main()
{
int i;
printf("\n");
}
printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<10; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
return 0;
}
Program 3: Write a C program to add two distances in inch- feet system
using structure.
#include <stdio.h>
struct Distance
{
int feet;
float inch;
} d1, d2, sumOfDistances;
int main()
{
printf("Enter information for 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
sumOfDistances.feet = d1.feet+d2.feet;
sumOfDistances.inch = d1.inch+d2.inch;
if (sumOfDistances.inch>12.0)
{
sumOfDistances.inch = sumOfDistances.inch-12.0;
++sumOfDistances.feet;
}
int main()
{
complex n1, n2, temp;
return 0;
}
return(temp);
}
int main()
{
struct TIME startTime, stopTime, diff;
// Calculate the difference between the start and stop time period.
differenceBetweenTimePeriod(startTime, stopTime, &diff);
return 0;
}
struct student_college_detail
{
int college_id;
char college_name[50];
};
struct student_detail
{
int id;
char name[20];
float percentage;
// structure within structure
struct student_college_detail clg_data;
}stu_data;
int main()
{
struct student_detail stu_data = {1, "Raju", 90.5, 71145,"Anna University"};
printf(" Id is: %d \n", stu_data.id);
printf(" Name is: %s \n", stu_data.name);
printf(" Percentage is: %f \n\n", stu_data.percentage);
return 0;
}
void printBook( struct Books *book )
{
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
}
union student
{
char name[20];
char subject[20];
float percentage;
};
int main()
{
union student record1;
union student record2;
strcpy(record2.subject, "Physics");
printf(" Subject : %s \n", record2.subject);
record2.percentage = 99.50;
printf(" Percentage : %f \n", record2.percentage);
return 0;
}
Program 4: WAP to display the total memory size occupied by the union
Solution:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
Program 5: WAP to access different members of a union using dot
operator.
Solution:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d", sizeof(uJob));
printf("\nsize of structure = %d", sizeof(sJob));
return 0;
}
Program 7:Write a program to display age and weight of a person using
pointer to structures.
Solution:
#include <stdio.h>
struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
personPtr = &person1; // Referencing pointer to memory address of
person1
return 0;
}
Program 8:WAP to access structure member through pointer using
dynamic memory allocation
Solution:
#include <stdio.h>
#include <stdlib.h>
struct person {
int age;
float weight;
char name[30];
};
int main()
{
struct person *ptr;
int i, num;
printf("Displaying Information:\n");
for(i = 0; i < num; ++i)
printf("%s\t%d\t%.2f\n", (ptr+i)->name, (ptr+i)->age, (ptr+i)->weight);
return 0;
}
Program 9:Write a program to store record of an employee using nested
structure.
Solution:
#include<stdio.h>
struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
};
struct Employee
{
int Id;
char Name[25];
float Salary;
struct Address Add;
};
void main()
{
int i;
struct Employee E;
printf("\nDetails of Employees");
printf("\n\tEmployee Id : %d",E.Id);
printf("\n\tEmployee Name : %s",E.Name);
printf("\n\tEmployee Salary : %f",E.Salary);
printf("\n\tEmployee House No : %s",E.Add.HouseNo);
printf("\n\tEmployee City : %s",E.Add.City);
printf("\n\tEmployee House No : %s",E.Add.PinCode);
}
Program 10:Write a program access members of a union through pointer
Solution:
union test
{
int x;
char y;
};
int main()
{
union test p1;
p1.x = 65;
// p2 is a pointer to union p1
union test *p2 = &p1;
}
Dynamic memory management functions (malloc, Calloc, Realloc and
Free), Memory leak
Q1: Write a C program to input and print text using Dynamic Memory
Allocation.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
char *text;
/*Free Memory*/
free(text);
return 0;
}
Q2: Write a C program to read a one dimensional array, print sum of all
elements along with inputted array elements using Dynamic Memory
Allocation.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr;
int limit,i;
int sum=0;
if(arr==NULL)
{
printf("Insufficient Memory, Exiting... \n");
return 0;
}
printf("Enter %d elements:\n",limit);
for(i=0; i<limit; i++)
{
printf("Enter element %3d: ",i+1);
scanf("%d",(arr+i));
/*calculate sum*/
sum=sum + *(arr+i);
}
Q3: Write a C program to read and print the student details using structure and
Dynamic Memory Allocation.
#include <stdio.h>
#include <stdlib.h>
/*structure declaration*/
struct student
{
char name[30];
int roll;
float perc;
};
int main()
{
struct student *pstd;
.
if(pstd==NULL)
{
printf("Insufficient Memory, Exiting... \n");
return 0;
}
return 0;
}
Q4: write a C program to create memory for int, char and float variable at run
time.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *iVar;
char *cVar;
float *fVar;
iVar=(int*)malloc(1*sizeof(int));
cVar=(char*)malloc(1*sizeof(char));
fVar=(float*)malloc(1*sizeof(float));
return 0;
}
Q5: Write a C program find largest element Using Dynamic Memory
Allocation.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, num;
float *data;
if(data == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
printf("\n");
return 0;
}
Q6: Write a program to copy a string to another using dynamic memory
allocation concept.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *mem_alloc;
/* memory allocated dynamically */
mem_alloc = calloc( 15, sizeof(char) );
int main()
{
char *mem_alloc;
/* memory allocated dynamically */
mem_alloc = malloc( 20 * sizeof(char) );
do {
a = fgetc(fp1);
fputc(a, fp2);
} while (a != EOF);
fcloseall();
getch();
}
Output:
#include<stdio.h>
int main() {
FILE *fp;
char ch;
int num;
long length;
fp = fopen("test.txt", "r");
if (fp == NULL) {
puts("cannot open this file");
exit(1);
}
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, (length - num), SEEK_SET);
do {
ch = fgetc(fp);
putchar(ch);
} while (ch != EOF);
fclose(fp);
return(0);
}
Output:
#include<stdio.h>
#include<process.h>
void main() {
FILE *fp1, *fp2;
char a;
clrscr();
do {
a = fgetc(fp1);
a = toupper(a);
fputc(a, fp2);
} while (a != EOF);
fcloseall();
getch();
}
#include<stdio.h>
int main() {
FILE *fp1, *fp2;
int ch1, ch2;
char fname1[40], fname2[40];
if (fp1 == NULL) {
printf("Cannot open %s for reading ", fname1);
exit(1);
} else if (fp2 == NULL) {
printf("Cannot open %s for reading ", fname2);
exit(1);
} else {
ch1 = getc(fp1);
ch2 = getc(fp2);
if (ch1 == ch2)
printf("Files are identical n");
else if (ch1 != ch2)
printf("Files are Not identical n");
fclose(fp1);
fclose(fp2);
}
return (0);
}
5. Write a C Program to Write on Data File and Read From Data File
#include<stdio.h>
struct Student {
int roll;
char name[12];
int percent;
} s1 = { 10, "SMJC", 80 };
int main() {
FILE *fp;
struct Student s2;
fp = fopen("ip.txt", "r");
fread(&s2, sizeof(s2), 1, fp);
fclose(fp);
return (0);
}
Output:
Roll : 10
Name : SMJC
Percent : 80
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main() {
FILE *fp1, *fp2;
char ch;
clrscr();
fp1 = fopen("Sample.txt", "r");
fp2 = fopen("Output.txt", "w");
while (1) {
ch = fgetc(fp1);
if (ch == EOF)
break;
else
putc(ch, fp2);
}
#include<stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen(__FILE__, "r");
do {
ch = getc(fp);
putchar(ch);
} while (ch != EOF);
fclose(fp);
return 0;
}
Output:
#include<stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen(__FILE__, "r");
do {
ch = getc(fp);
putchar(ch);
} while (ch != EOF);
fclose(fp);
return 0;
}
int main ()
{
FILE * fptr;
int i,n;
char str[100];
char fname[20];
char str1;
void main()
{
FILE *fold1, *fold2, *fnew;
char ch, fname1[20], fname2[20], fname3[30];
return 0;
}
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
C Program to Read from a text file using fscanf()
#include <stdio.h>
int main()
{
int num;
FILE *fptr;
fscanf(fptr,"%d", &num);
return 0;
}
#include <stdio.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:\\program.bin","wb")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
for(n = 1; n < 5; ++n)
{
num.n1 = n;
num.n2 = 5n;
num.n3 = 5n + 1;
fwrite(&num, sizeof(struct threeNum), 1, fptr);
}
fclose(fptr);
return 0;
}
C program to Reading from a binary file using fread()
#include <stdio.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
return 0;
}