Title: Strings
1Strings
- Representation of strings.
- Manipulation of strings.
2What are strings ?
- A string is an array of chracters terminated by
'\0' which is a single character with the value
of 0 in ASCII, (this is different from the '0'
character, which is the value 48 in ASCII). - A string constant is a group of characters
between double quotes, e.g. the string "hello". - The string "hello" is stored in memory as an
array of the following characters 'h', 'e', 'l',
'l', 'o', '\0'. Although hello is 5 characters
long it occupies an array of 6 characters in
memory. The additional character is the
terminating character '\0'.
3How to represent strings ?
As a character array e.g. the string "hi" char
str3 str0 'h' str1 'i' str2
'\0' Or using array initialization char str3
"hi" OR char str "hi" (in the second
statement array size is automatically
calculated), in this case the '\0' is
automatically appended, and the array is 3
characters long. The above is allowed only for
array initialzation, e.g. char str3 str
"hi" is a syntax error. This is assignment and
NOT initialization which is allowed on an element
by element basis, like the first example above.
As a pointer to char e.g. the string "hi" char
str str "hi" Notice in this case assignment
is allowed. Note Pointer has to point to an
array of characters, before it can be used in
other statements, this is done with the second
statement above.
Using the string class e.g. the string
"hi" string str str "hi" OR Using
initialization string str("hi") Here str is a
string class object. Note Must use include
4I/O operations
- Input a string from the input stream (keyboard)
- Using char array
- cin.getline(array name, number of characters to
be read) - e.g. char str20 cin.getline(str, 15)
- this will read characters from the input stream
(keyboard) into the array, until the newline
character or until 14 characters are read, then
it appends \0 - cin array name
- e.g. char str20 cin str
- this will read characters from the input stream
(keyboard) into the array, it stops at the first
white space, may cause array overflow. - Output a string to the output stream (the screen)
- cout
- cout
- cout
- e.g. char str1 "hi" char str2 "hi"
string str3 "hi" - cout
-
5Passing strings as function arguments
- As a char array (the array size could be defined
or not) - e.g. void read_write(char str20 )
- cout
-
- OR
- void read_write(char str )
- cout
-
- in both cases the array str is initialized to
the actual argument passed to the function when
it is called, the second case is more flexible as
it allows the array to accomodate variable
lengths of strings. - As a char pointer
- e.g. void read_write(char str)
- cout
-
- in this case the pointer is initialized to point
to the first byte of the actual string passed to
the function. - As a string class object
- e.g. void read_write(string str)
- cout
-
6Manipulating strings
- strcpy (param1, param2)
- param1 is a char array name, param2 is a char
array name, a constant string, or a char pointer
initialized to point to a char array, strcpy
copies the string of param2 into the array of
param1 (Note check arrays sizes and don't forget
the '\0' !). - e.g. char pc "hello" char str6
strcpy(str, pc) - // here str size is 6 chracters to accomodate
'h', 'e', 'l', 'l', 'o', '\0' - e.g. char pc "hello" char str6
strcpy(str, pc) - e.g. char str6 strcpy(str, "hello")
- strlen (param)
- param is a char array name, a constant string,
or a char pointer initialized to point to a char
array. This function returns the number of
characters in the array (NOT COUNTING the '\0') - e.g. char pc "hello" strlen(pc) // will
return 5 - strcmp(param1, param2)
- param1 and param2 can be a char array name, a
constant string, or a char pointer initialized to
point to a char array, strcmp will return 0 if
the two strings are identical. - e.g. char pc "hello" strcmp(pc, "hello")
// will return 0 - e.g. char pc"hi" char str3"hi"
strcmp(pc, str) // will return 0
7Example1 (using char)
include using namespace std /
function to store a string on the heap, this
function will accept a string (char array) and it
will store this string on the heap and return a
pointer to the char array created on the heap /
char store_string(char str) char s s
new charstrlen(str)1 // determine size of
array on heap strcpy(s, str) // initialize
array on heap return s int main(void) char
pc pc store_string("hi there!") cout screen delete pc // free memory (the
array) return 0
8Character functions in
9Some string handling functions in
10Example 2 using string
void main( void ) string str("aa") callByValue
(str) cout cout t World" cout
include include void callByValue(string a) a "call by value" cout void callByReference(string a) a "call by reference" cout void callByPointer(string a) a "call by pointer" cout 11string members
www.cppreference.com