Chapter 22
Chapter 22
Chapter 22
C Strings, Characters, and Bit Manipulation
Objectives
• To assign, concatenate, compare, search, and swap strings
• To understand the string characteristics
• To find, replace, and insert characters in strings
• To write programs that check the spelling, calculate the string length, and
perform word matching
3
#include <iostream>
#include <cstring> // prototypes for strcpy and strncpy
using namespace std; Note: \0 indicates the
int main() end of the string
{
char x[] = "Happy Birthday to You"; // string length 21
char y[ 25 ];
char z[ 15 ];
strcpy( y, x ); // copy contents of x into y
cout << "The string in array x is: " << x << "\nThe string in array y is: "
<< y << '\n'; Note: this appending
step is unnecessary in
// copy first 14 characters of x into z this case, however, if the
strncpy( z, x, 15 ); // copies 15 characters from x to z number 15 in strncpy
z[ 15 ] = '\0'; // append '\0' to z's contents was 17 (larger than the
cout << "The string in array z is: " << z << endl; size of z) then appending
is necessary
} // end main
5
#include <iostream>
#include <cstring> // prototypes for strcpy and strncpy
using namespace std;
int main() Note: cin.get helps to
{ input a sentence that
char x[25]; contains spaces, cin alone
cin.get(x,25); cannot!
char y[ 25 ];
char z[ 15 ];
strcpy( y, x ); // copy contents of x into y
cout << "The string in array x is: " << x << "\nThe string in array y is: "
<< y << '\n';
} // end main
7
#include <iostream>
#include <cstring> // prototypes for strcat and strncat
using namespace std;
int main()
{
char s1[ 20 ] = "Happy "; // length 6
char s2[] = "New Year "; // length 9
char s3[ 40 ] = "";
cout << "s1 = " << s1 << "\ns2 = " << s2;
strcat( s1, s2 ); // concatenate s2 to s1 (length 15)
cout << "\n\nAfter strcat(s1, s2):\ns1 = " << s1 << "\ns2 = " << s2;
// concatenate first 6 characters of s1 to s3
strncat( s3, s1, 6 ); // concatenate 6 characters
cout << "\n\nAfter strncat(s3, s1, 6):\ns1 = " << s1 << "\ns3 = " << s3;
strcat( s3, s1 ); // concatenate s1 to s3
cout << "\n\nAfter strcat(s3, s1):\ns1 = " << s1 << "\ns3 = " << s3 << endl;
} // end main
9
#include <iostream>
#include <iomanip>
#include <cstring> // prototypes for strcmp and strncmp
using namespace std;
int main()
{
char s1[] = "Happy New Year";
char s2[] = "Happy New Year";
char s3[] = "Happy Holidays";
cout << "s1 = " << s1 << "\ns2 = " << s2 << "\ns3 = " << s3
<< "\n\nstrcmp(s1, s2) = " << setw( 2 ) << strcmp( s1, s2 )
<< "\nstrcmp(s1, s3) = " << setw( 2 ) << strcmp( s1, s3 )
<< "\nstrcmp(s3, s1) = " << setw( 2 ) << strcmp( s3, s1 );
int main()
{
char string1[] = "abcdefghijklmnopqrstuvwxyz";
char string2[] = "four";
char string3[] = "Boston";
cout << "The length of \"" << string1 << "\" is " << strlen( string1 )
<< "\nThe length of \"" << string2 << "\" is " << strlen( string2 )
<< "\nThe length of \"" << string3 << "\" is " << strlen( string3 )
<< endl;
} // end main
13
Character-handling Library
• The character-handling library includes several functions that perform
useful tests and manipulations on characters such as islower,
isupper, isdigit
u converted to uppercase is U
L converted to lowercase is l
15
cout << "\nu converted to uppercase is " << static_cast< char >( toupper( 'u' ) )
<< "\nL converted to lowercase is " << static_cast< char >( tolower( 'L' ) );
} // end main
16
Exercise
What is the output?
#include <iostream>
Answer:
using namespace std; rac
caa
int main()
{
char a[3] = {'c', 'a', 'r'};
cout << a[2] << " " << a[1] << " " << a[0] << endl;
a[2] = a[1];
cout << a[0] << " " << a[1] << " " << a[2] << endl;
}
17
Exercise
What is the output?
#include <iostream>
Answer:
using namespace std; 1
0
int main()
{
int n=0;
cout << !n << endl;
cout << (n == 3);
}
18
Exercise
What is the output?
Bitwise Operators
• The bitwise operators are: bitwise AND (&), bitwise inclusive OR (|), bitwise exclusive
OR (^), left shift (<<), right shift (>>) and bitwise complement (~)
21
#include <iostream>
#include <iomanip> OUTPUT
using namespace std; The result of combining the following
int main() 6
{ 3
unsigned number1; unsigned mask; using the bitwise AND operator & is
number1 = 6; mask = 3; 2
7
cout << "The result of combining the following\n"; 12
cout << number1 << endl; 3
cout << mask << endl;
cout << "using the bitwise AND operator & is" << endl;
cout << (number1 & mask) << endl; // demonstrate bitwise AND (&)
cout << (number1 | mask) << endl; // demonstrate bitwise OR (|)
cout << (number1 << 1) << endl; // demonstrate left shift one place, ~ multiply by 2
cout << (number1 >> 1) << endl; // demonstrate right shift one place, ~ divide by 2
} // end main