The document discusses C strings and the string class in C++. It provides information on:
- C strings being null-terminated character arrays
- Declaring and initializing C string variables
- Common C string functions like strlen(), strcpy(), strcmp()
- The C++ string class which handles memory allocation dynamically and supports operators like + and +=
- Key differences between C strings and the string class like null termination and size handling
2. Jeanine Ingber
C Strings
A C string is a sequence of characters,
terminated with a null character (‘0’)
value of null is 0
All C strings are assumed to be null
terminated
“This is a string” is an example of a string
constant. This constant is null terminated.
3. Jeanine Ingber
C string variables
C++ has no built in string data type
C strings are supported using a 1
dimensional array of characters.
Since a string must be terminated by a null,
the size of a string variable (array of type
char) must be declared to hold to one
additional character (the ‘0’)
4. Jeanine Ingber
Declaring and Initializing C strings
char string1[10];
– can hold 10 characters
– can hold a string of up to 9 characters in length
char name[] = “Ingber”;
– name is declared to have size 7
– name is a C string
char string2[10] = {‘t’, ‘o’, ‘m’};
– string2 is declared to have size 10
– string2 is not a C string
char string3[10] = {‘s’, ‘u’, ‘e’, ‘0’};
– string3 is declared to have size 10
– string3 is a C string
5. Jeanine Ingber
Aggregate Operations
Aggregate array assignment is not allowed in C++
char name[] = “Sue”; //valid initialization
char name2[10];
name2 = “Tom”; //not allowed
C++ does support aggregate I/O for strings only
char name[] = “Jack”;
cout << name; //Jack is output
int numbers[10] = {1,2,3};
cout << numbers; //what will it output?
char string[5] = {‘h’,’e’, ‘l’, ‘l’, ‘o’};
cout << string; //what will it output?
6. Jeanine Ingber
String Input
>>
– uses white space as a delimiter
– appends null to input string
– does not do any array bounds checking
.getline(char* string, int limit, [char delim = ch])
– extracts a block of characters and places them in the
character array addressed by string
– extracts at most limit-1 characters
– appends the null character
– extracts less than limit-1 characters if end-of-file is reached
or delim character is reached
– by default, the delimiter is the newline (‘n’)
7. Jeanine Ingber
Examples using >>
char first[5], last[5];
cin >> first >> last;
cout << first << last;
input stream:
Mary Smith
first
last
Outut?
M a r y 0
S m i t h
8. Jeanine Ingber
>> example
char text_string[10];
cin >> text_string;
cout << text_string << endl;
cin.getline(text_string, 10);
cout << text_string << endl;
input stream
This is a line of text.
Output?
9. Jeanine Ingber
Examples using getline()
const int lineSize = 72;
int readin, lcnt=0;
char text_string[lineSize];
fin.getline(text_string, lineSize);
while(!fin.eof())
{
lcnt++;
readin = fin.gcount(); //returns the number of characters
//actually extracted by last call to getline()
cout << “Line #” << lcnt << “tChars read: “ << readin <<endl
<< text_string << endl;
fin.getline(text_string, lineSize);
} //sample of a few lines from a math text
Line #1 Chars read: 48
Mathematics is an expression of the human mind.
Line #2 Chars read: 57
It reflects the active will, the contemplative reason ..
Line #3 Chars read: 12
Never mind.
10. Jeanine Ingber
C String Functions
Defined in the header file string.h
(=, <, >, ==) not defined for C strings
strlen(str)
– returns integer length of string
– counts all characters up to but not including null
strcmp(str1, str2)
– returns integer < 0 if str1 < str2
– returns integer == 0 if str1 == str2
– returns integer > 0 if str1 > str2
strcpy(to_str, from_str)
– copies from_str including null into to_str
strcat(str1,str2)
– append str2 to the end of str1
11. Jeanine Ingber
Function to Swap two C String
void Swap(char str1[], char str2[])
{
char temp[MAXLEN];
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
}
.
if(strcmp(str1,str2) > 0)
swap(str1, str2);
12. Jeanine Ingber
2-D Arrays of Char - Array of
Strings
char word_list[word_count][max_word_len];
cin >> word_list[0]; //read word and store in first row
strcpy(word_list[1], “Hello”);
H e l l o 0
13. Jeanine Ingber
String Class - include <string>
string objects have two attributes
– the characters in the string
– the number of characters in a string
string objects are not ‘0’ terminated
string message = “Hello”;
message
size
H e l l o
5
14. Jeanine Ingber
String I/O
>>, <<
string name; //no size required
cin >> name; //uses white space as a delimiter
cout << name;
getline()
string textline;
getline(cin, textline, ‘n’);
cout << textline;
16. Jeanine Ingber
String Functions
size()
– returns the number of characters in a string
substr(int start, int len)
– returns the sub-string of length len, beginning at start
find(string sub_str, int start)
– returns the position of sub_str (or a very large number if sub_str is
not found)
– begins looking for sub_str at start
c_str()
– converts string object to C string
17. Jeanine Ingber
#include <iostream>
#include <string>
using namespace std;
int main()
{
string date = “March 1, 1999”;
int i = date.size(); // i is assigned the value 13
string year;
year = date.substr(9,4);
cout << year << endl; //output is 1999
i = date.find(‘,’ , 0); // i is assigned 7
return 0;
}
18. Jeanine Ingber
Example - Palindromes
A palindrome is a word (noon) , sentence
(Draw a level award) or number (18781)
that reads the same backward or forward.
Write a function that takes a string as input
and determines if the string is a
palindrome. (Note: white space is ignored
when determining if a word, sentence or
number is a palindrome. All white space
has been removed from the input string.)
19. Jeanine Ingber
/*------------------------------------------------------------
-----*/
/* This function returns a value of 1 if string is a
palindrome. */
/* The function returns a value of 0 otherwise.
*/
int is_palindrome(char string[])
{
int is_pal, begin=0, end;
end = strlen(string) -1;
is_pal = 1;
while(is_pal && begin <=end)
{
if(string[begin] != string[end])
{
is_pal = 0;
}
begin++;
end--;
}
return(is_pal);
}
/*------------------------------------------------------------
---*/