0% found this document useful (0 votes)
34 views12 pages

Adv C Slip Only Quesn

The document contains 10 code snippets demonstrating the use of structures in C programming. The snippets cover topics like defining structures, accepting user input into structures, accessing structure members, passing structures to functions, dynamic memory allocation, file handling operations on structures, and arrays of structures.
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)
34 views12 pages

Adv C Slip Only Quesn

The document contains 10 code snippets demonstrating the use of structures in C programming. The snippets cover topics like defining structures, accepting user input into structures, accessing structure members, passing structures to functions, dynamic memory allocation, file handling operations on structures, and arrays of structures.
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/ 12

Slip 1:-

Write a C program to accept details of n students (roll number, name,


percentage) using structure. Display details of the student having
maximum percentage.
#include <stdio.h>
struct student {
charfirstName[50];
int roll;
float marks;
} s[10];
int main() {
int i;
printf("Enter information of students:\n");
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
OUTPUT:-
Enter information of students:

For roll number1,


Enter name: Tom
Enter marks: 98

For roll number2,


Enter name: Jerry
Enter marks: 89

Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
Slip 2:-
Write a menu driven program to perform the following operations on
strings using standard library functions:
1. Convert string to uppercase
2. Copy one string to another
3. Compare two strings
4. Concatenate two strings
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char s1[50],s2[50];
int ch,i;
printf("\nMenu :\n 1.Convert string to uppercase\n");
printf(" 2. Copy one string to another\n");
printf("3. Compare two strings\n 4. Concatenate two strings\n");
printf("5. Exit\n Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter String: ");
scanf("%s",s1);
printf("\n Converted String: ");
for(i=0;i<strlen(s1);i++)
printf("%c",toupper(s1[i]));
break;
case 2: printf("\n Enter String: ");
scanf("%s",s1);
strcpy(s2,s1);
printf("\n Copied String is : %s",s2);
break;
case 3: printf("\n Enter string1 : ");
scanf("%s",s1);
printf("\n Enter String2 : ");
scanf("%s",s2);
if(strcmp(s1,s2)==0)
printf("\n Entered strings are equal...");
else
printf("\n Entered strings are different..");
break;
case 4: printf("\n Enter string1 : ");
scanf("%s",s1);
printf("\n Enter string2 : ");
scanf("%s",s2);
printf("\n Concatenated string : %s",strcat(s1,s2));
break;
case 5: exit(0);
break;
default: printf("\n Enter proper choice");
} //end switch
return 0;
}

Slip 3:-
Write a program to declare a structure person (name, address) which
contains another structure birthdate (day, month, year). Accept the details
of n persons and display them.
#include <stdio.h>
struct student{
char name[30];
introllNo;
structdateOfBirth{
intdd;
int mm;
intyy;
}DOB;
};
int main()
{
struct student std;
printf("Enter name: "); gets(std.name);
printf("Enter roll number: "); scanf("%d",&std.rollNo);
printf("Enter Date of Birth [DD MM YY] format: ");
scanf("%d%d%d",&std.DOB.dd,&std.DOB.mm,&std.DOB.yy);
printf("\nName : %s \nRollNo : %d \nDate of birth : %02d/%02d/%02d\
n",std.name,std.rollNo,std.DOB.dd,std.DOB.mm,std.DOB.yy);

return 0;
}
OUTPUT:-
Enter name: Mike
Enter roll number: 101
Enter Date of Birth [DD MM YY] format: 14 03 92

Name : Mike
RollNo : 101
Date of birth : 14/03/92

Slip:-4
Write a program to copy contents of file a.txt to b.txt by changing the
case of each alphabet.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int main()
{
FILE *fp1,*fp2;
char ch;
fp1=fopen("a.txt","r");
fp2=fopen("b.txt","w");
if(!fp1)
printf("\n Error in reading..");
if(!fp2)
printf("\n Error in writing..");
else
{
while((ch=fgetc(fp1))!=EOF)
{
if(isupper(ch))
fputc(tolower(ch),fp2);
else
fputc(toupper(ch),fp2);
}
}
return 0;
}

Slip:-5
Write a program to read the contents of a text file and count the number of
characters, lines and words in the file.
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * file;
char path[100];
char ch;
int characters, words, lines;
file=fopen("counting.txt","w");
printf("enter the text.press cntrl Z:\n");
while((ch = getchar())!=EOF){
putc(ch,file);
}
fclose(file);
printf("Enter source file path: ");
scanf("%s", path);
file = fopen(path, "r");
if (file == NULL){
printf("\nUnable to open file.\n");
exit(EXIT_FAILURE);
}
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF){
characters++;
if (ch == '\n' || ch == '\0')
lines++;
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
if (characters > 0){
words++;
lines++;
}
printf("\n");
printf("Total characters = %d\n", characters);
printf("Total words = %d\n", words);
printf("Total lines = %d\n", lines);
fclose(file);
return 0;
}
Output
enter the text.press cntrl Z:
Hi welcome to Tutorials Point
C programming Articles
Best tutorial In the world
Try to have look on it
All The Best
Enter source file path: counting.txt

Total characters = 116


Total words = 23
Total lines = 6

Slip 6:-
Write a C program to create structure employee (id, name, salary). Accept
details of n employees and perform the following operations:
a. Display all employees.
b. Display details of all employees having salary > ____.

#include<stdio.h>

#include<stdlib.h>

struct details

char name[30];

int eid;

int salary;

}emp[5];

void emp_search(int r)

int id,i;

printf("\nEnter Employee-Id to be Searched : ");

scanf("%d",&id);

printf("----------------------------------------\n");

for(i=0;i<r;i++)

if(emp[i].eid==id)

printf("Employee Id : %d",emp[i].eid);

printf("\nName : %s",emp[i].name);

printf("\nSalary : %d\n",emp[i].salary);

}
}

void display(int r)

int i;

printf("\nList of All Employees:\n");

printf("-------------------------------\n");

printf("Emp-Id\tEmp-Name Salary\n");

printf("--------------------------------\n");

for(i=0;i<r;i++)

printf("%d\t%s\t %d\n",emp[i].eid,emp[i].name,emp[i].salary);

void greater(int r)

int i;

printf("\nDetails of Employee Whose Salary > 10000\n");

printf("------------------------------------------------");

for(i=0;i<r;i++)

if(emp[i].salary>10000)

printf("\n Employee Name : %s",emp[i].name);

printf("\n Employee-Id : %d",emp[i].eid);


printf("\n Salary : %d\n",emp[i].salary);

int main()

int n,i,ch;

printf("/*How Many Employee Record You Want to Add*/\n\nEnter Limit : ");

scanf("\n %d",&n);

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

printf("-----------------------------------------");

printf("\n\tEnter Details of Employee-%d",i+1);

printf("\n-----------------------------------------");

printf("\nName of Employee : ");

scanf("%s",emp[i].name);

printf("Employee-Id : ");

scanf("%d",&emp[i].eid);

printf("Salary : ");

scanf("%d",&emp[i].salary);

while(1)

printf("-----------------------------------------\n");

printf("\t\tMenu\n");
printf("-----------------------------------------");

printf("\n 1:Search Employee by E-ID");

printf("\n 2:List of All Employee");

printf("\n 3:Display Employee Name whose Salary > 10000 ");

printf("\n 4:Exit");

printf("\n----------------------------------------\n");

printf("Enter Your Choice : ");

scanf("\n %d",&ch);

switch(ch)

case 1: emp_search(n);

break;

case 2: display(n);

break;

case 3: greater(n);

break;

case 4: exit(0);

return 0;

}
Slip 7:-

Write a C program to accept n elements using dynamic memory allocation


and calculate the sum and average of the elements. Also find the largest
element.

Slip 8:-

Write a C program to accept details of n items (code, name, price) using


structure. Perform the following operations:
a. Display all items having price > ___
b. Search for an item by name.

Slip 9:-

Write a C program to create a structure named student that accepts student


rollno, name and marks of 3 subjects. Calculate the total of marks and
percentage. Create an array of structures to enter information for n
students and display the details.

Slip 10:-

Write C program to copy contents of one file to another by changing case


of each alphabet and replacing digits by *

You might also like