4. Generally speaking, a string is a sequence of characters
• Examples: “hello”, “high school”, “H2O”.
DEFINITION OF STRINGS
“Hello"
“High school"
5. DECLARATION OF STRINGS
The following instructions are all equivalent.
They declare x to be an object of type string,
and assign the string “high school” to it:
string x(“high school”);
8. TYPE OF STRING
C++ provides following two types of string representations,
The C-style character string (#include<string.h> Or #include<cstring>).
The string class type introduced with Standard C++ (#include<string>).
9. When we use C-string Functions so, we use this library.
(#include<string.h> Or #include<cstring>)
When we use String Class Functions so, we use this library.
(#include<string>)
LIBRARY FOR C-STRING &
STRING CLASS
10. C-STRING
Strings are arrays of chars. String literals are words surrounded by double quotation marks.
The string in C programming language is actually a one-dimensional array of characters which is
terminated by a null character ‘0’. Thus a null-terminated string contains the characters that comprise
the string followed by a null.
A string can be declared as a character array or with a string pointer.
The following declaration and initialization create a string consisting of the word "Hello". To hold the
null character at the end of the array, the size of the character array containing the string is one more
than the number of characters in the word "Hello”.
OR
"This is a static string"
char aj[6]={‘H’,’E’,’L’,’L’,’O’,’0’} char aj[ ]={“HELLO”}
char *aj={“HELLO”}
11. Following is the memory presentation of above defined string in C/C++:
It's important to remember that there will be an extra character on the end on a string, literally a '0'
character, just like there is always a period at the end of a sentence. Since this string terminator is
unprintable, it is not counted as a letter, but it still takes up a space. Technically, in a fifty char array
you could only hold 49 letters and one null character at the end to terminate the string.
Actually, you do not place the null character at the end of a string constant. The C compiler
automatically places the '0' at the end of the string when it initializes the array.
STRING MEMORY
INDEX 0 1 2 3 4 5
VARIABLE ‘H’ ‘A’ ‘L’ ‘L’ ‘O’ ‘0’
ADDRESS 0*23451 0*23452 0*23453 0*23454 0*23455 0*23456
12. Five char array you could only hold 4
letters and one null character at the end to
terminate the string.
13. Six char array you could only hold
6 letters and one null character at the end
to terminate the string.
16. Sr. No Function & Purpose
strcpy(s1, s2);
Copies string s2 into string s1.
strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
strlen(s1);
Returns the length of string s1.
strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
C++ strncpy()
The strncpy() function in C++ copies a specified bytes of characters from source to destination.
strrev(s1, s2)
Reverse the string
C-STRING FUNCTIONS
21. String Comparison
This function compare 2 strings its returns
the ASCII difference of the first to non
matching char in both the strings.
Int strcmp(string1, string2)
If difference is equal to zero => srting1=string2
If difference is equal to +ve => srting1>string2
If difference is equal to -ve => srting1<string2
23. Without using strrev( )
To solve this problem, two standard
library functions strlen() and strcpy() are
used to calculate length and to copy string
respectively.
24. The string class is a specialization of a more general template class called
basic_string (Pre-define Class)[Child class string, wstring).
String is another container class (STL).
To use string class, you have to include string header. - #include<string>
Since defining a class in C++ is creating a new data type, string is derived data type.
STRING-CLASS
25. Careless programmer can overrun the end of an array that holds a null terminated
string.
For example, using strcpy()
String class handle such issues
STRING IS SAFE THAN CHAR ARRAY
33. FIND CONTENT IN STRING
Searches the string for the first
occurrence of the sequence specified
by its arguments.
34. FIND LAST OCCURRENCE OF
CONTENT IN STRING
Searches the string for the last
occurrence of the sequence
specified by its arguments.
35. String Comparison
This function compare 2 strings its returns
the ASCII difference of the first to non
matching char in both the strings.
If difference is equal to 0 => srting1=string2
If difference is equal to +ve => srting1>string2
If difference is equal to -ve => srting1<string2
36. RETURN LENGTH OF STRING
Returns the length of the string, in terms
of bytes.
Editor's Notes
Delete this slide when you finish preparing the other slides.