0% found this document useful (0 votes)
14 views21 pages

Chapter 22

Uploaded by

maryam osama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views21 pages

Chapter 22

Uploaded by

maryam osama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to Computer Programming (CSCI 112)

Chapter 22
C Strings, Characters, and Bit Manipulation

Dr. Ali Alnoman


Spring 2023

Reference: C++ How to Program, 10th edition, P. Deitel and H. Deitel


2

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

Copying Strings with strcpy and strncpy


• Function strcpy copies its second argument—a string—into its first
argument
• Function strncpy is much like strcpy, except that strncpy specifies
the number of characters to be copied from the string into the array
4

#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

The above example can be re-written using cin to input the


sentence “Happy Birthday to You” as follows:
6

#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';

// copy first 14 characters of x into z


strncpy( z, x, 15 ); // copies 14 characters from x to z
z[ 15 ] = '\0'; // append '\0' to z's contents
cout << "The string in array z is: " << z << endl;

} // end main
7

Concatenating Strings with strcat and strncat


• Function strcat appends its second argument (a string) to its first
argument (a character array containing a string)
• You must ensure that the array used to store the first string is large
enough to store the combination of the first string, the second string and
the terminating null character (copied from the second string)
• Function strncat appends a specified number of characters from the
second string to the first string and appends a terminating null character
to the result
8

#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

Comparing Strings with strcmp and strncmp


• Function strcmp compares its first string argument with its second
string argument character by character
1. The function returns zero if the strings are equal
2. a negative value if the first string is less than the second string
3. a positive value if the first string is greater than the second string

• Function strncmp is equivalent to strcmp, except that strncmp


compares up to a specified number of characters
10

#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 );

cout << "\n\nstrncmp(s1, s3, 6) = " << setw( 2 )


<< strncmp( s1, s3, 6 ) << "\nstrncmp(s1, s3, 7) = " << setw( 2 )
<< strncmp( s1, s3, 7 ) << "\nstrncmp(s3, s1, 7) = " << setw( 2 )
<< strncmp( s3, s1, 7 ) << endl;
} // end main
11

Comparing Strings with strcmp and strncmp


• How does the computer know that one letter comes before another?
• All characters are represented inside the computer as numeric codes; when
the computer compares two strings, it actually compares the numeric codes
of the characters in the strings
• With some compilers, functions strcmp and strncmp always return -1, 0 or 1
• With other compilers, these functions return 0 or the difference between the
numeric codes of the first characters in the strings being compared. For
example, N (in New Year) has a numeric code of 78, H (in Happy) has a
numeric code of 72. In this case, the return value will be 6 or -6
12

Determining String Lengths using strlen


#include <iostream>
#include <cstring>
using namespace std;

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

• Needs the <cctype> library

• The following program illustrates the usability of the <cctype> library


14

#include <iostream> OUTPUT


#include <cctype> // character-handling function prototypes
using namespace std;
According to isdigit:
8 is a digit
int main()
{
# is not a digit
cout << "According to isdigit:\n"
<<( isdigit( '8' ) ? "8 is a" : "8 is not a" ) << " digit\n" According to isalpha:
<<( isdigit( '#' ) ? "# is a" : "# is not a" ) << " digit\n"; A is a letter
& is not a letter
cout << "\nAccording to isalpha:\n"
<< ( isalpha( 'A' ) ? "A is a" : "A is not a" ) << " letter\n" According to islower:
<<( isalpha( '&' ) ? "& is a" : "& is not a" ) << " letter\n" ; p is a lowercase letter
P is not a lowercase letter

continues next slide... According to isupper:


D is an uppercase letter
d is not an uppercase letter

u converted to uppercase is U
L converted to lowercase is l
15

cout << "According to islower:\n"


<<( islower( 'p' ) ?"p is a" : "p is not a" ) << " lowercase letter\n"
<<( islower( 'P' ) ?"P is a" : "P is not a" ) << " lowercase letter\n";

cout << "\nAccording to isupper:\n"


<<( isupper( 'D' ) ?"D is an" : "D is not an" ) << " uppercase letter\n"
<<( isupper( 'd' ) ?"d is an" : "d is not an" ) << " uppercase letter\n";

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?

#include <iostream> Answer:


using namespace std;
358
int main()
{
int arr[4] = {2,3,4,5};
int i;
for (i=1; i <= 3; i++)
{
arr[i] = arr[i-1] + i;
cout << arr[i] << " ";
}
}
(FOR INFORMATION ONLY)
20

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

You might also like