Unit V
Unit V
Strings:
A string is a group or collection of characters which are enclosed between double
quotes.
A string is a character array in C language.
Declaration of strings:
To declare string variables, we must declare character array in C language.
Syntax:
char string_name[size];
Here string_name is any valid identifier and size represents the maximum no. of
characters that are allowed in that string variable.
Example:
char s1[10];
char s2[20];
Initialization of string variables:
To initialize a string variable, the following syntax is used.
Syntax:
char string_name[size]=”string value”;
Example:
char s1[5]=”GOOD”;
G O O D \0
When C compiler assigns the value to the string variable, C compiler adds a null
character(\0) at the end of the string.
Strings can also be initialized by passing individual characters as follows
char s1[5]={‘G’,’O’,’O’,’D’,’\0’};
char s1[]={‘G’,’O’,’O’,’D’,’\0’};
Reading strings from the keyboard:
To read strings from the keyboard, we use scanf function with %s as the format
specifier.
Example:
char s1[10];
scanf(“%s”, s1);
The problem with scanf function is, when the white space is encountered the scanf
function is terminated without reading the characters after the white space. So scanf
function is not suitable for accepting the strings with white spaces.
Reading a line of text:
To read a line of text from the keyboard, we use getchar function repeatedly until the
use presses enter key.
Example:
char s1[20], ch;
int i=0;
ch=getchar();
while(ch!=’\n’)
{
s1[i]=ch;
i++;
ch=getchar();
}
C language provides one standard library function in stdio.h header file to read a line of
text from the keyboard i.e., gets.
Syntax:
gets(string_name);
Example:
char s1[20];
printf(“enter a string”);
gets(s1);
Syntax:
int strlen(string_name);
Example:
char s1[7]=”VISHNU”;
int n=strlen(s1); //n=6
2. strcat():
This function is used to perform concatenation of two strings.
Concatenation of two strings means adding one string at the end of another
string.
Syntax:
strcat(string1, string2);
Here string2 will be added at the end of string1 and result will be stored in
string1.
Example:
char s1[10]=”VISHNU”;
char s2[10]=”COLLEGE”;
strcat(s1,s2);
After concatenation
s1 is
V I S H N U C O L L E G E \0
s2 is
C O L L E G E \0
3. strncat():
This function is used for performing concatenation of first n number of
characters from one string with another string.
Syntax:
strncat(string1, string2,n);
Example:
char s1[10]=”VERY”;
char s2[15]=”WELL DONE”;
strncat(s1, s2, 4);
After concatenation
s1 is
V E R Y W E L L \0
s2 is
W E L L D O N E \0
4. strcpy():
This function is used to copy from one string to another string.
Syntax:
strcpy(string1, string2);
Here string1 is a destination string and string2 is a source string.
The destination string should be a string variable and the source string can be
either string variable or string value.
Example1:
char s1[5]=”GOOD”;
char s2[5];
strcpy(s2,s1);
Example2:
char s1[5];
strcpy(s1, “GOOD”);
6. strcmp():
This function is used to perform comparison of two strings.
Syntax:
int strcmp(string1, string2);
This function returns integer value, i.e., either zero or positive value or negative
value.
This function returns zero when string1 and string2 are equal.
This function returns positive value when string1 is alphabetically greater than
string2.
This function returns negative value when string1 is alphabetically less than
string2.
Example:
int n=strcmp(“Vishnu”, “Vishnu”); //n=0
int x=strcmp(“there”, “their”); //x=9
int y=strcmp(“BAD”, “BAT”); //y=-16
7. stricmp():
This function is also used for performing comparison of two strings by ignoring
case sensitive letters.
Syntax:
stricmp(string1,string2);
Example:
int x=stricmp(“Vishnu”, “Vishnu”); //x=0
8. strrev():
This function is use to reverse all the characters in the given string.
Syntax:
strrev(string_name);
Example:
char s1[4]=”GOD”;
strrev(s1);
10. strupr():
This function is used to convert all the characters in the given string into upper
case letters.
Syntax:
strupr(string_name);
Example:
char s1[5]=”good”;
strupr(s1); //s1=”GOOD”
Write a program to find the length of the string using library function.
/*program to find the length of the string using library function*/
#include<stdio.h>
void main()
{
char s1[10];
printf(“enter a string”);
scanf(“%s”,s1);
printf(“Length of the string %s is %d”,s1, strlen(s1));
}
Write a program to copy from one string into another string using library
function.
/*program to copy from one string into another string using library function*/
#include<stdio.h>
void main()
{
char s1[15], s2[15];
printf(“enter a string”);
scanf(“%s”,s1);
strcpy(s2,s1);
printf(“After copying string2 is %s”,s2);
}
Write a program to concatenate two strings using library function.
/*program to concatenate two strings using library function*/
#include<stdio.h>
void main()
{
char s1[20], s2[20];
printf(“ enter first string”);
scanf(“%s”,s1);
printf(“enter second string”);
scanf(“%s”,s2);
strcat(s1, s2);
printf(“After concatenation string1 is %s\n”,s1);
printf(“String2 is %s”,s2);
}
Write a program to compare two strings using library function.
/*program to compare two strings using library function*/
#include<stdio.h>
void main()
{
char s1[20], s2[20];
int x;
printf(“enter first string”);
scanf(“%s”, s1);
printf(“enter second string”);
scanf(“%s”, s2);
x=strcmp(s1, s2);
if(x==0)
printf(“strings are equal”);
else if(x>0)
printf(“s1 is greater than s2”);
else
printf(“s1 is less than s2”);
}
Write a program to check whether the given string is palindrome or not using
library function.
/*program to check whether the given string is palindrome or not using library
function*/
#include<stdio.h>
void main()
{
char s1[20], s2[20];
printf(“enter a string”);
scanf(“%s”, s1);
strcpy(s2, s1);
strrev(s1);
if(strcmp(s1, s2)==0)
printf(“Given string is palindrome”);
else
printf(“Given stirng is not palindrome”);
}
Structures:
Structure is a collection of heterogeneous elements under a single name
(or)
Structure is a collection of different types of elements that are stored under a single
name.
Creating a structure (or) Declaration of a structure:
In C language, structures can be created by using the keyword struct.
Syntax:
struct structure_name
{
datatype member1;
datatype member2;
.
.
.
Datatype membern;
};
Here structure_name is any valid identifier and each structure member is declared
along with its datatype.
Structure declaration should be ended with semicolon.
Example1:
struct student
{
int rollno;
char name[20];
int marks;
};
Example2:
struct employee
{
int empid;
char empname[20];
float salary;
};
Creating a structure variable:
There are two ways for creating a structure variable.
1.
Syntax:
struct structure_name var1, var2,………varn;
Example:
struct student s1,s2;
struct employee e1, e2;
2. It is possible to create structure variable at the time of structure declaration.
Syntax:
struct structure_name
{
datatype member1;
datatype member2;
.
.
.
datatype membern;
}var1, var2,…..varn;
Example:
struct student
{
int rollno;
char name[20];
int marks;
}s1, s2;
Accessing structure members:
Structure members can be accessed by using structure variable and dot(.) operator. Dot
operator is also called as member access operator.
Syntax:
structure_var_name.member_name
Example:
s1.rollno
s1.name
s1.marks
Assigning values to structure members:
s1.rollno=1;
s1.name=”abc”;
s1.marks=80;
To read structure members from the keyboard, we can use scanf function as follows
scanf(“%d%d%d”,&s1.rollno, s1.name, &s1.marks);
Initialization of structures:
Structures can be initialized in two ways.
1.
Syntax:
struct structure_name
{
datatype member1;
datatype member2;
.
.
.
datatype membern;
} var_name={values};
Example:
struct student
{
int rollno;
char name[20];
int marks;
} s1={1, “abc”, 90};
2.
Syntax:
struct structure_name var_name={values};
Example:
struct student s1={1, “abc”, 80};
Write a program to create a student structure which contains rollno, name and
marks as structure members and read the student details and display them.
/*program to read student details and display those details*/
#include<stdio.h>
struct student
{
int rollno;
char name[20];
int marks;
};
void main()
{
struct student s1;
printf(“enter rollno, name and marks”);
scanf(“%d%s%d”, &s1.rollno, s1.name, &s1.marks);
printf(“student details are\n”);
printf(“%d\t%s\t%d”, s1.rollno, s1.name, s1.marks);
}
Write a C Program to Store Information of a Movie Using Structure
/*program to store information of a movie using structure*/
#include<stdio.h>
struct movie
{
char title[15];
char director[15];
char hero_name[15];
int duration;
};
void main()
{
struct movie m1;
printf(“enter movie details”);
scanf(“%s%s%s%d”, s1.title, s1.director, s1.hero_name, &s1.duration);
printf(“Movie details are\n”);
printf(“Title=%s\nDirector=%s\nHero=%s\nDuration=%d\n”, m1.title, m1.director,
m1.hero_name, m1.duration);
}
Structures and Pointers:
To access structure members using pointers, we have to create pointer variable to the
structure by using the following syntax
struct structure_name *ptr_name;
To access structure members using pointers, pointer name and arrow (->) operator is
used. Arrow operator is also called as indirection member access operator.
Syntax:
ptr_name->member_name
Syntax:
struct structure_name1
{
datatype member1;
datatype member2;
struct structure_name2
{
datatype member3;
datatype member4;
} var1;
} var2;
We can access the outer structure members by using var2 and inner structure
members by using outer structure variable, inner structure variable and dot
operator as var2.var1.member3
Example:
Write a program to illustrate the use of nested structures.
/*program to illustrate the use of nested structures*/
#include<stdio.h>
struct student
{
int rollno;
struct name
{
char fname[15];
char mname[15];
char lname[15];
} n;
int age;
};
void main()
{
struct student s;
printf(“enter rollno, name and age”);
scanf(“%d%s%s%s%d”, &s.rollno, s.n.fname, s.n.mname, s.n.lname, &s.age);
printf(“Student details are\n”);
printf(“%d\t%s\t%s\t%s\t%d”, s.rollno, s.n.fname, s.n.mname, s.n.lname, s.age);
}
2. Writing structure variable as member of another structure:
In this, we are writing one structure variable as a member of another structure.
Syntax:
struct structure_name2
{
datatype member3;
datatype member4;
};
struct structure_name1
{
datatype member1;
datatype member2;
struct structure_name2 var1;
} var2;
Write a program to illustrate the use of nested structures.
/*program to illustrate the use of nested structures*/
#include<stdio.h>
struct name
{
char fname[15];
char mname[15];
char lname[15];
};
struct student
{
int rollno;
struct name n;
int age;
};
void main()
{
struct student s;
printf(“enter rollno, name and age”);
scanf(“%d%s%s%s%d”, &s.rollno, s.n.fname, s.n.mname, s.n.lname, &s.age);
printf(“Student details are\n”);
printf(“%d\t%s\t%s\t%s\t%d”, s.rollno, s.n.fname, s.n.mname, s.n.lname, s.age);
}
Write a program to read the name and date of birth of a person and display the
person details using nested structures.
/*program to display person details using nested structures*/
#include<stdio.h>
struct person
{
char name[20];
struct dob
{
int day;
int month;
int year;
}d;
};
void main()
{
struct person p;
printf(“enter name and date of birth of a person”);
scanf(“%s%d%d%d”, p.name, &p.d.day, &p.d.month, &p.d.year);
printf(“Person details are\n”);
printf(“Name=%s\n Date of Birth=%d-%d-%d\n”, p.name, p.d.day, p.d.month,
p.d.year);
}
Array of structures:
In order to process several student records, employee details, we can use array of
structures.
Array of structures can be created by using the following syntax
Syntax:
struct structure_name array_name[size];
Example:
struct student s[10];
Write a program to create a structure named as student which contains rollno,
name and grade as structure members. Display the rollno, name and grade of n
students by using array of structures.
#include<stdio.h>
struct student
{
int rollno;
char name[20];
char grade;
};
void main()
{
struct student s[20];
int n, i;
printf(“enter number of students”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“enter rollno,name and grade”);
scanf(“%d%s%c”, &s[i].rollno, s[i].name, &s[i].grade);
}
printf(“Student details are\n”);
printf(“Rollno\tName\tGrade\n”);
for(i=0;i<n;i++)
{
printf(“%d\t%s\t%d\n”, s[i].rollno, s[i].name, s[i].grade);
}
}
Write a program to create student structure which contains rollno, name and 5
subjects marks as structure members. Display the total marks of each student
along with its rollno and name by using array of structures.
#include<stdio.h>
struct student
{
int rollno;
char name[20];
int marks[5];
int total;
};
void main()
{
struct student s[20];
int n, i, j;
printf(“enter number of students”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“enter rollno, name”);
scanf(“%d%s”, &s[i].rollno, s[i].name);
s[i].total=0;
for(j=0;j<5;j++)
{
printf(“enter subject %d marks”,j+1);
scanf(“%d”, &s[i].marks[j]);
s[i].total=s[i].total+s[i].marks[j];
}
}
printf(“Student details are\n”);
printf(“Rollno\tName\tTotal\n”);
for(i=0;i<n;i++)
{
printf(“%d\t%s\t%d\n”, s[i].rollno, s[i].name, s[i].total);
}
}
Structures and Functions:
Like ordinary variables, structures can also be passed as an argument to sub functions.
In C language, there ae 3 ways for passing structures to functions.
1) Passing individual structure members as arguments
2) Passing structure variable as argument
3) Passing structure by address
1. Passing individual structure members as arguments:
In this, we are passing individual structure members as actual arguments to sub
functions.
Example:
#include<stdio.h>
struct student
{
int rollno;
char name[20];
int age;
};
void display(int x, char y[], int z);
void main()
{
struct student s1;
printf(“enter rollno,name and age”);
scanf(“%d%s%d”, &s1.rollno, s1.name, &s1.age);
display(s1.rollno, s1.name, s1.age);
}
void display(int x, char y[], int z)
{
printf(“Student details are\n”);
printf(“%d\t%s\t%d\n”, x, y, z);
}
Self-referential structures are used to connect the records of same type by using their
addresses.
Generally self-referential structures are used to create data structures like linked lists,
stakcs, queues, trees and graphs.
Example:
#include<stdio.h>
struct A
{
int i;
struct A *p;
};
void main()
{
struct A a1, a2, a3, *temp;
a1.i=10;
a2.i=20;
a3.i=30;
a1.p=&a2;
a2.p=&a3;
a3.p=NULL;
temp=&a1;
while(temp!=NULL)
{
printf(“%d”,temp->i);
temp=temp->p;
}
}
UNIONS:
Union is a collection of different types of elements that are stored under a single name.
Unions can be created by using the keyword union.
Syntax:
union union_name
{
datatype member1;
datatype member2;
.
.
.
datatype membern;
};
Example:
union A
{
int a;
float b;
char c;
};
In order to access union members, we have to create union variable by using the
following syntax
Syntax:
union union_name var1, var2, ……….varn;
We can access union members by using union variable and dot operator.
Example:
union A a1;
a1.a
a1.b
a1.c
Write a program to illustrate the use of unions.
/*program to illustrate the use of unions*/
#include<stdio.h>
union A
{
int a;
float b;
char c;
};
void main()
{
union A a1;
printf(“enter a value”);
scanf(“%d”, &a1.a);
printf(“a=%d”, a1.a);
printf(“enter b value”);
scanf(“%f”,&a1.b);
printf(“b=%f”, a1.b);
printf(“enter c value”);
scanf(“%c”, &a1.c);
printf(“c=%c”, a1.c);
}
Write a program to determine the size of the union.
/*program to determine the size of the union*/
#include<stdio.h>
union B
{
int x;
char y[20];
float z;
};
void main()
{
union B b1;
printf(“Size of the union is %d”, sizeof(b1));
}
Differences between structure and union:
Structure Union
1)Structure is a collection of different 1)Union is a collection of different types
types of elements that are stored under of elements that are stored under a
a single name. single name.
2)Structures can be created using the 2)Unions can be created using the
keyword struct. keyword union.
3)Each structure member has its own 3)All the union members sharing the
memory address. common memory space
4)All the structure members can be used 4)Only one union member can be used at
at a time any point of time.
5)Memory size of structure is the sum of 5)Memory size of union is the memory
the memory sizes of all structure size of the largest union member
members
6)structure members can be accessed by 6)union members can be accessed by
using structure variable and dot using union variable and dot operator
operator
typedef:
typedef keyword is used for creating a new name for the existing datatype.
Syntax:
typedef datatype identifier;
Here datatype represents any one of the primary datatype and identifier represents a
new name for the existing datatype.
Example1:
typedef int xyz;
xyz a, b;
Example2:
typedef float abc;
abc x, y;
Example3:
typedef struct student
{
--------------
------------
------------
}s1;
s1 a, b;
Bit-fields:
Bit-field is a set of adjacent bits whose size is in between 1 to 16.
Bit field is applicable to only for integers.
Bit-fields can be used only for structure members or union members.
Syntax:
struct structure_name
{
datatype member1: bit-length;
datatype member2: bit-length;
.
.
.
datatype membern: bit-length;
};
Here datatype may be int or signed int or unsigned int and bit-length represents the no.
of bits required to store the value in that member.
We cannot use scanf function for reading structure members which are declared with
bit-fields.
Example:
#include<stdio.h>
struct dob
{
unsigned int day: 5;
unsigned int month: 4;
unsigned int year: 7;
};
void main()
{
struct dob d;
d.day=3;
d.month=5;
d.year=22;
printf(“Date of Birth=%d-%d-%d”, d.day, d.month, d.year);
}