The document discusses strings in C and C++. It explains that strings are not a built-in data type in C/C++ and describes C-style strings as character arrays terminated by a null character. It also discusses C++ string classes like std::string. The document provides examples of using C-style strings and C++ strings. It describes common string functions in C++ for manipulating and comparing strings.
2. Strings
Strings are a fundamental concept, but they are not a built-in data type in C/C++.
String is collection of characters.
C-Strings
◦ C-style string: character array terminated by first null character.
◦ Wide string: wide character array terminated by first null character.
C++ - Strings
◦ Several Classes
◦ Standard Template Class:
◦ std::basic_string, std::string, std::wstring
No inter-operability between C and C++ style strings.
3. String example
C-string
◦ Array of chars that is null terminated (‘0’).
C++ - string
• Object whose string type is defined in the <string> file has a large repertoire of functions (e.g. length,
replace, etc.)
char cs[ ] = “Napoleon”; // C-string
string s = “Napoleon”; // C++ - string
cout << s << “ has “ << s.length() << “ characters.n”;
s.replace(5, 2,”ia”); //changes s to “Napolian
4. Strings
C-style strings consist of a contiguous sequence of characters
terminated by and including the first null character.
◦ A pointer to a string points to its initial character.
◦ The length of a string is the number of bytes preceding the null character
◦ The value of a string is the sequence of the values of the contained characters,
in order.
h e l l o 0
length
5. C++ Strings
◦ Formatted Input: Stream extraction operator
◦ cin >> stringObject;
◦ the extraction operator >> formats the data that it receives through its input stream; it skips over whitespace
◦ Unformatted Input: getline function for a string
◦ getline( cin, s)
◦ does not skip over whitespace
◦ delimited by newline
◦ reads an entire line of characters into s
string s = “ABCDEFG”;
getline(cin, s); //reads entire line of characters into s
char c = s[2]; //assigns ‘C’ to c
S[4] = ‘*’; //changes s to “ABCD*FG”
7. 7
Functions
Character Macro Description
isalpha Returns true (a nonzero number) if the argument is a letter of the
alphabet. Returns 0 if the argument is not a letter.
isalnum Returns true (a nonzero number) if the argument is a letter of the
alphabet or a digit. Otherwise it returns 0.
isdigit Returns true (a nonzero number) if the argument is a digit 0–9.
Otherwise it returns 0.
islower Returns true (a nonzero number) if the argument is a lowercase
letter. Otherwise, it returns 0.
isprint Returns true (a nonzero number) if the argument is a printable
character (including a space). Returns 0 otherwise.
ispunct Returns true (a nonzero number) if the argument is a printable
character other than a digit, letter, or space. Returns 0
otherwise.
isupper Returns true (a nonzero number) if the argument is an uppercase
letter. Otherwise, it returns 0.
isspace Returns true (a nonzero number) if the argument is a whitespace
character. Whitespace characters are any of the following:
8. Program Code(ACSII)
// This program demonstrates some of the character testing
// functions.
#include <iostream.h>
#include <ctype.h>
void main(void)
{
char input;
cout << "Enter any character: ";
cin.get(input);
cout << "The character you entered is: " << input << endl;
cout << "Its ASCII code is: " << int(input) << endl;
if (isalpha(input))
cout << "That's an alphabetic character.n";
if (isdigit(input))
cout << "That's a numeric digit.n";
if (islower(input))
cout << "The letter you entered is lowercase.n";
if (isupper(input))
cout << "The letter you entered is uppercase.n";
if (isspace(input))
cout << "That's a whitespace character.n";
}
Enter any character: A
The character you entered is: A
Its ASCII code is: 65
That's an alphabetic character.
The letter you entered is uppercase.
OUTPUT
Enter any character: 7 [Enter]
The character you entered is: 7
Its ASCII code is: 55
That's a numeric digit.
OUTPUT
9. 9
Character Case Conversion
The C++ library offers functions for converting a character to upper or lower case.
◦ Be sure to include ctype.h header file
Function Description
toupper Returns the uppercase equivalent of its argument.
tolower Returns the lowercase equivalent of its argument.
10. 10
Sample Code
// This program calculates the area of a circle. It asks the
// user if he or she wishes to continue. A loop that
// demonstrates the toupper function repeats until the user
// enters 'y', 'Y', 'n', or 'N'.
#include <iostream.h>
#include <ctype.h>
void main(void)
{
const float pi = 3.14159;
float radius;
char go;
cout << "This program calculates the area of a circle.n";
cout.precision(2);
cout.setf(ios::fixed);
do
{
cout << "Enter the circle's radius: ";
cin >> radius;
cout << "The area is " << (pi * radius * radius);
cout << endl;
do
{
cout << "Calculate another? (Y or N) ";
cin >> go;
} while (toupper(go) != 'Y' && toupper(go) != 'N');
} while (toupper(go) == 'Y');
}
Enter the circle's radius: 10
The area is 314.16
Calculate another? (Y or N) b
Calculate another? (Y or N) y
Enter the circle's radius: 1
The area is 3.14
Calculate another? (Y or N) n
OUTPUT
11. 11
String Function
Function Description
strlen Accepts a C-string or a pointer to a string as an argument. Returns the length of the
string (not including the null terminator. Example Usage: len = strlen(name);
strcat Accepts two C-strings or pointers to two strings as arguments. The function appends
the contents of the second string to the first string. (The first string is altered, the
second string is left unchanged.) Example Usage: strcat(string1, string2);
strcpy Accepts two C-strings or pointers to two strings as arguments. The function copies
the second string to the first string. The second string is left unchanged. Example
Usage: strcpy(string1, string2);
strncpy Accepts two C-strings or pointers to two strings and an integer argument. The third
argument, an integer, indicates how many characters to copy from the second string
to the first string. If the string2 has fewer than n characters, string1 is padded with
'0' characters. Example Usage: strncpy(string1, string2, n);
strcmp Accepts two C-strings or pointers to two string arguments. If string1 and string2are
the same, this function returns 0. If string2 is alphabetically greater than string1, it
returns a negative number. If string2 is alphabetically less than string1, it returns a
positive number. Example Usage: if (strcmp(string1, string2))
strstr Accepts two C-strings or pointers to two C-strings as arguments, searches for the
first occurrence of string2 in string1. If an occurrence of string2 is found, the
function returns a pointer to it. Otherwise, it returns a NULL pointer (address 0).
Example Usage: cout << strstr(string1, string2);