POP Unit 4
POP Unit 4
IN C
22CS2ESPOP - UNIT 4
Prof. SNEHA S BAGALKOT
Assistant Professor, Dept. of CSE
UNIT – 4: Strings and Structures
▪Strings: Introduction
▪Operations on Strings (Length of a String,
▪Converting Lowercase to Uppercase and Vice Versa
▪String Concatenation, String Comparison)
▪Example Programs
Strings: Introduction
• A string is a null-terminated character array.
• Declaration Syntax: char str[size];
• Example: char str[] = “HELLO”; H E L L O \0
• Char str[]=“H”; H \0
• Char ch=‘H’; H
• Char str[]=“ ”; \0
• The name of the character array (or the string) is a pointer to the
beginning of the string.
• If we declare str as, char str[5] = “HELLO”;
• The size of the string should be equal to maximum number of
characters in the string plus one.
Introduction (Contd..)
• Similar to arrays, subscripts are used to access the elements of the
character array. The subscript starts with a zero.
• The other way to initialize a string :
char str[ ]={‘H’,’E’,’L’,’L’,’O’,’\0’};
• We can also declare a string with size much larger than the number of
elements that are initialized.
• char str[10]=”HELLO”;
H E L L O \0 \0 \0 \0 \0 \0
while( a[x] )
{ Output:
putchar( a[x]);
x++; This is CG Section, BMSCE!
}
putchar('\n');
}
int main()
{
char string[] = "This is CG Section, BMSCE!";
display(string);
return(0);
}
PART B: LAB PROGRAM 3
▪Structures: Introduction
▪Arrays of Structures
▪Nested Structures
▪Example Programs
Structures: Introduction
• Structure is a user-defined data type that can store related information.
void main()
{
struct book b1={"Reema Thareja", "Programming in C", 516.6, 2016};
printf("\n Book Author is: %s",b1.author_name);
printf("\n Book Name is: %s",b1.book_title);
printf("\n Book price is: %f",b1.price);
printf("\n Year of publication is: %d",b1.year);
}
Partial initialization
void main()
{
struct book b1={"Reema Thareja", "Programming in C"};
printf("\n Book Author is: %s",b1.author_name);
printf("\n Book Name is: %s",b1.book_title);
printf("\n Book price is: %f",b1.price);
printf("\n Year of publication is: %d",b1.year);
}
Write a C Program to define a structure to store book
information with member elements as follows:
To store author name as char array
To store book title as char array
To store year as integer
To store price as float
In the above example an array called book is defined, that consist of 100 elements.
Each element is defined to be that of type struct bookbank.
Question
Write a C program using structures to read and display the information
of N students.
The structure should have following member elements
USN of the student: Character array
Name of student: Character array
Marks of the student: Floating point number
Sample Input and Output
Enter N value: 2
Enter USN: 1BM18CS001
Enter Name: Aditya
Enter marks: 40
Enter USN: 1BM18CS002
Enter Name: Bharath
Enter marks: 38
Displaying Information:
USN: 1BM18CS001
Name: Aditya
Marks: 40.00
USN: 1BM18CS002
Name: Bharath
Marks: 38.00
#include<stdio.h>
void main()
{
struct student {
char usn[15];
char name[25];
float marks;
};
struct student s[10];
int i, n;
printf("Enter N value: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter USN: ");
scanf("%s", s[i].usn);
printf("Enter Name: ");
scanf("%s", s[i].name);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("\n Displaying Information:\n");
for(i = 0; i<n; i++)
{
printf("USN: %s\n", s[i].usn);
printf("Name: %s\n", s[i].name);
printf("Marks: %f\n", s[i].marks);
}
}
Nested Structures
STRUCTURES WITHIN STRUCTURE
A structure within a structure is called Nested Structure
Syntax:
struct struct_name {
datatype var1;
datatype var2;
Note:
struct struct_name1 {
By using outer structure variable only, it is
datatype var3; possible to access the variables of inner
datatype var4; structure.
}variable1; outer_struct . inner_struct . datamember;
}variable;
struct struct_name variable;
STRUCTURES WITHIN STRUCTURE
Example:
struct bookbank {
char author[50]; It can be accessed as:
char title[50]; book. author
struct bookbank1 { book. title
int year; book. details. year
float price; book. details. price
}details;
}book;
Write a program to demonstrate nested structure.
Create a structure which holds student roll number ,
name and course. Let name be a structure which has
members first name middle name and last name.
Read and display 1 student information
#include<stdio.h>
struct student
{
int r_no;
struct NAME
{
char f_name[20];
char m_name[20];
char l_name[20];
}n;
char course[20];
};
void main()
{
struct student s;
printf("enter roll number");
scanf("%d", &s. r_no);
printf("enter name of the student");
scanf("%s", s. n. f_name);
printf("\n student roll and name is \n%d\n%s", s. r_no, s. n. f_name);
}
Write a program to demonstrate nested structure.
Create a structure which holds student roll number ,
name and course. Let name be a structure which has
members first name middle name and last name.
Read and display 5 student’s information
#include<stdio.h>
struct student
{
int r_no;
struct NAME
{
char f_name[20];
char m_name[20];
char l_name[20];
} n;
char course[20];
};
void main()
{
struct student s[50];
int i, m;
printf("how many student detail you wish to enter?");
scanf("%d", &m);
for(i=0; i<m; i++)
{
printf("enter roll number first name and last name");
scanf("%d%s%s",&s[i].r_no, s[i].n.f_name, s[i].n.m_name);
}
Structures
Implement structures to read, write and
compute the average salary for a department
of N employees. (Consider nested structure
for DOB)
Output:
Question
Implement structures to read, write, compute
average- marks and the students scoring above and
below the average marks for a class of N students
Sample Input and Output
Input:
Enter number of students:2
Enter USN: 1BM18CS001
Enter Name: Arjun
Enter Marks: 40
Enter USN: 1BM18CS002
Enter Name: Bharath
Enter Marks: 60
Output:
Arjun scored below average
Bharath scored above average
#include <stdio.h> for(i=0; i<n; i++) {
main() { printf("\n Enter USN: ");
struct student scanf("%s", s[i]. usn);
{ printf("\n Enter Name: ");
char usn[15]; scanf("%s", s[i]. name);
char name[25]; printf("Enter Marks: \n");
float marks; scanf("%f", &s[i]. marks);
}; }
struct student s[20]; for(i=0; i<n; i++) {
int n, i; sum = sum + s[i]. marks;
float avg, sum=0.0; }
avg=sum/n;
printf("Enter number of students:"); for(i=0; i<n; i++)
scanf("%d", &n); {
if(s[i].marks >= avg)
printf("\n %s scored above average", s[i].name);
else
printf("\n %s scored below average", s[i].name);
}
}
Using sizeof operator to find size of a structure
struct book
{
char name[20];
char title[20];
float price;
int year;
};
void main()
{
struct book b1;
printf("Size of the structure is %d", sizeof(b1));
}
Example Programs