Unit 3 - String
Unit 3 - String
STRINGS
1
Strings
• A string is a collection of the individual array elements or characters.
2
NULL Character
• NULL Character is also known as string terminating
character.
• It is represented by “\0”.
3
Declaration of a string
• Since we cannot declare string using String Data Type, instead of which we use
array of type “char” to create String.
• Syntax :
• Examples :
• char city[30];
• char name[20];
• When we are using string for other purpose than accepting and printing data then
4
you must include following header file in your code– #include<string.h>
Initializing String (Character Array)
• Process of Assigning some legal default data to String is Called
Initialization of String.
• A string can be initialized in different ways. We will explain this
with the help of an example.
• Below is an example to declare a string with name as str and
initialize it with “Welcome”.
1. char str[] = “Welcome";
2. char str[50] = “Welcome";
3. char str[] = {‘W’,'e’,’l’,’c’,’o’,’m’,’e’,'\0’};
4. char str[14] = {‘W’,'e’,’l’,’c’,’o’,’m’,’e’,’\0’};
5
STRING EXAMPLE
#include <stdio.h>
int main ()
return 0;
Syntax : strcmp (str1 , str2 );strcmp( ) function is case sensitive. i.e, “A” and
“a” are treated as different characters.
10
String Length (strlen)
• strlen( ) function in C gives the length of the given string.
• Syntax : strlen(str);
11
• Lab Programs
• Concatenate two strings without built-in functions
• Reverse a string using built-in and without built-in string functions
• https://mitsacin-my.sharepoint.com/:v:/g/personal/
deepthip_mits_ac_in/EfGov8dnlyxFs91YS2cHegwB5Yvy95-
UmTLNwvqg9L0mSw?referrer=Teams.TEAMS-
ELECTRON&referrerScenario=MeetingChicletGetLink.view.view
12