U-3 String Handling Functions& U-4 Structures in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

U-3

String Handling Functions

Presented by
R.Leela Jyothi
strlwr():
 It is used to convert all upper case letter
in string to lower case .
 Syntax:
strlwr( string);
Program:
 #include<stdio.h>
 #include<string.h>
 int main()
 {
 char str[20];
 printf("Enter string:\n");
 gets(str);
 strlwr(str);
 printf("String in lowercase is:");
 puts(str);
 return 0;
 }
Output:
 Enter string:
WELCOME TO C LAB
String in lowercase is: welcome to c lab
strupr():
 It is used to convert all lower case letter
in string to upper case.
 Syntax
strupr( string);
Program:
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
printf("Enter string:\n");
gets(str);
strupr(str);
printf("String in uppercase is:");
puts(str);
return 0;
}
Output:
Enter string:
welcome to c lab
String in uppercase is:WELCOME TO C
LAB
strcmp():
 This function is used to compare the string
arguments.
 It compares both the strings character by
character. It starts comparing the very first
character of strings until the characters of
both strings are equal or NULL character
is found.
 If the first character of both strings are
equal, it checks second character and so
on. This process will be continued until
NULL character is found or both
characters are unequal.
Syntax

integer_variable = strcmp( string1,


string2);
Program:
#include<stdio.h>
#include<string.h>
int main()
{
char str1[40], str2[40];
int d;
printf("Enter first string:\n");
gets(str1);
printf("Enter second string:\n");
gets(str2);
 d = strcmp(str1, str2);
 if(d==0)
 {
 printf("Given strings are same.");
 }
 else
 {
 printf("Given strings are different.");
 }
 return 0;
 }
Output:
 Enter first string: Welcome to C↲ Enter
second String: Welcome to C↲
 Given strings are same.
 Enter first string: Welcome↲ Enter
 second String: Welcome to C↲
 Given strings are different
U-4
Structures
Structures in C
 It is a collection of variables of different
datatypes which are logically grouped
and are referred under a single name .
 It is a user defined datatype.
How to implement Structures
 Define a structure.
 Declare structure variables.
 Initialize members of structures.
 Accessing members of structures.
Define a Structure:
Syntax:
struct struct-name
{ // Member1
DataType1 MemberName1;
// Member2
DataType2 MemberName2;
// Member N
DatatypeN MemberNameN;
};
Eg
 If we have to define a structure for a
student, then its related information
would be roll_number, name,
percentage.
 struct student
{
int rno;
char name[10];
float percentage;
};
Declare structure variables
 Syntax:
struct <struct-name> variable name;
 Eg: struct student stud1;
 It is done in 2 ways:
1. In structure declaration.
2. Using the structure tag.
In structure declaration
struct student
{
int rno;
char name;
float percentage;
} stud1;

variable of student type.


Using the structure tag:
struct student
{
int rno;
char name;
float percentage;
};
struct student stud1;
Initializing members of structure
 Syntax:
struct structure-name structure-
variable={ value1,value2,…..value N};
 C does not allow the initialization of
individual structure members within
the structure definition.
Eg:
 struct student
{
int rno;
char name;
float percentage;
};
struct student stud1={8,
”Gowtham”,70.2};
Accessing data members
 Syntax:
structure-variable.member-name;
 To access members of structure, we use
dot(.) symbol.
 Eg:
stud1. rno;
stud1.name;
stud1.percentage;
Example program
#include<stdio.h>
struct student
{
int rno;
char name;
float percentage;
};
Contin..
void main()
{
struct student stud1;
printf(“enter student details”);
scanf(“%d%s%f”,
&stud1.rollno,stud1.name,
&stud1.percentage);
printf(“student details”);
printf(“\t %d \t %s\t
%f”,stud1.rollno,stud1.name,stud1.percentage);
}

You might also like