ICP-PART-8
ICP-PART-8
ICP-PART-8
ICP-PART-8
About strings
Representation in C
String Literals
String Variables
String Input/Output
– printf, scanf, gets, puts
String Functions
– strlen, strcpy, strncpy, strcmp, strncmp, strcat,
strncat, strchr, strrchr, strstr, strspn, strcspn, strtok
Introduction
A string is an array of characters.
– Individual characters are stored in memory in ASCII
code.
A string is represented as a sequence of characters
terminated by the null (‘\0’) character.
I I I T N R \0
[0The] null
[1] character
[2] [3]is not
[4] counted
[5] [6] towards[10]the length of
the strings, but takes memory of one element
Null character cannot be read or written but it is used
internally only
Reading and Writing a String
A string can be read and written by using
single scanf and printf function
%s(string)
No loop is needed
Example:
char name[30];
::
scanf (“%s”, name);
Contd.
The ampersand (&) is not required before the variable
name with “%s”.
– “name” represents an address.
The scanf function reads a string from the keyboard till a
whitespace character(blank,tab,newline) is encountered.
After reading is complete, the null character is appended
automatically at the next position inside the array.
scanf (“%s”, name);
If we type “IIIT NR”
name will be assigned the string “IIIT”
Contd..
If we are interested to read a string consisting of blank
character as well as, we want to read a line of text.
gets() function is used
string.h header le must be included to use gets()
Similarly for printing a string puts function can be used.
Syntax:
gets(string name)
puts(string)
Contd.
Analogy-2
If I take you to a parking lot and ask you to nd me a red car, you
extend one nger (of your choice ;)) and point to a red car.
– Your nger is not the answer. Your nger tells me nothing, but if
I look where you're nger is pointing to, I can nd what I was
looking for.
– Now I can ask you to nd a blue car and you can redirect your
nger (reassign) it to a new car. Now your pointer (the same one
Example
Pointers in C-Introduction
A pointer is a variable that represents the
location (rather than the value) of a data item.
They have a number of useful applications.
– Enables us to access a variable that is de ned outside
the function.
– Can be used to pass information back and forth
between a function and its reference point.
– More e cient in handling data tables.
– Reduces the length and complexity of a program.
– Sometimes also increases the execution speed.
Basic Concept
In memory, every stored data item occupies one
or more contiguous memory cells(bytes).
– The number of bytes required to store a data item
depends on its type (char, int, oat, double, etc.).
Whenever we declare a variable, the system
allocates memory location(s) to hold the value of
the variable.
– Since every byte in memory has a unique address, this
location will also have its own (unique) address.
Contd.
Consider the statement
– int xyz = 50;
This statement instructs the compiler to allocate
a location for the integer variable xyz, and put
the value 50 in that location.
Suppose that the address location chosen is
1380.
xyz =variable
50 =value
1380 =address
Contd.
During execution of the program, the system
always associates the name xyz with the
address 1380.
– The value 50 can be accessed by using either the
name xyz or the address 1380.
Since memory addresses are simply numbers,
they can be assigned to some variables which
can be stored in memory.
– Such variables that hold memory addresses are
called pointers.
– Since a pointer is a variable, its value is also stored
Contd.
Suppose we assign the address of xyz to a
“pointer” variable p.
– p is said to point to the variable xyz.
p = &xyz;
Address vs. Value
Each memory cell has an address associated with it.
Each cell also stores some value.
Don’t confuse the address referring to a memory location
with the value stored in that location.
* symbol is used to get the value of the variable that the pointer is
pointing to.
If pointer is assigned to NULL, it means it is pointing to nothing.