Lecture 17

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Today’s lecture outline

• Strings
• Null character
• Memory map of string
• String initialization
• Display string content
• Input string from user
• Passing a string to a function

1
String
• A group of integers can be stored in an integer
array
• Similarly a group of characters can be stored
in a character array
• Character arrays are many a time also called
strings
• Character arrays or strings are used to
manipulate text such as words and sentences

2
Cont.
• A string is a one-dimensional array of
characters terminated by a null ( ‘\0’ )

• Each character in the array occupies one byte


of memory
• The last character is always ‘\0’

3
Null character
• Represented by a back slash and a zero
• \0
• It looks like two characters, but it is actually
only one character
• Note that ‘\0’ and ‘0’ are not same. ASCII
value of ‘\0’ is 0, whereas ASCII value of ‘0’ is
48

4
Memory allocation for string
• The elements of the character array are stored in
contiguous memory locations
H A E S L E R \0
65508 65509 65510 65511 65512 65513 65514 65515

• The terminating null (‘\0’) is important, because it is


the only way to know where the string ends
• A string not terminated by a ‘\0’ is not really a string,
but just a collection of characters

5
String initialization

• C/C++ will automatically insert \0 character at the


end of the string.
• Memory allocated for name string is 8 bytes
• 7bytes for actual string and 1 byte for \0 character

6
How string element are displayed

7
Cont.

K l i n s m a n \0
65508 65509 65510 65511 65512 65513 65514 65515 65515

8
Pointer to string

65508
65510
65509
*ptr 65512
65513
65511

*ptr = *(65508) = h
h ! = \0 true name[6] h e l l o \0
e ! = \0 true 65508 65509 65510 65511 65512 65513
l ! = \0 true
Program output
l ! = \0 true
o ! = \0 true he l lo
\0 ! = \0 false
9
Cont.
• Difference
– We cannot assign one string in another string
– But we can assign one char pointer to another
char pointer

10
Passing string to a function

*q 65508
65510
65509
65512
65513
65511

str1[6] h e l l o \0
65508 65509 65510 65511 65512 65513

Program output
he l lo 11
Standard Library String Functions
• With every C compiler a large set of useful
string handling library functions are provided
• strlen( ), to find length of string
• strcpy( ), to copy content of one string in another
• strcat( ) to append content of one string after the
content of other string
• strcmp( ) to compare one string with another

12
strlen( )
• Short for string length
• This function counts the number of characters
present in a string
• The base address of the string must be pass when
calling strlen function
• char str1[ ] = “hello”;
• strlen(str1)  5 not 6
• \0 is not the part of string it is just character that tells
where this string terminates
str1[6] h e l l o \0
65508 65509 65510 65511 65512 65513
13
strlen – program

14
strcpy( )
• This function copies the contents of one string
into another
• The base addresses of the source and target
strings should be supplied to this function

15
In C++:Caution with cin
• The length of the string should not exceed the
dimension of the character array
• cin is not capable of receiving multi-word strings. E.g.
hello world
• cin.getline( ) function can be used to input multi-
word strings

16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy