Prelab5 String+Operators
Prelab5 String+Operators
1. Strings
1.1 Objectives
➔ Become familiar with string declaration, initialization, and manipulation using the standard
library in C++.
➔ Understand basic string functions like concatenation, input handling, and common errors.
In C++, strings are part of the Standard Library and can be manipulated using built-in functions.
Below are the most common string operations.
You can declare strings using std::string. Uninitialized strings are empty by default.
Example:
Use cin to read strings, but it only reads up to the first space. To read an entire line, use
getline().
However, mixing cin and getline() can cause issues due to how each function handles the
newline character.
● cin: Reads input until it encounters a space or newline but leaves the newline in the
buffer.
● getline(): Reads input until a newline character (\n) is encountered, which may
cause it to read an empty line if there's leftover input from a previous cin.
To avoid this issue when switching between cin and getline(), use cin.ignore() to
discard the leftover newline character:
CS 100 Computational Problem Solving
Substring: Use substr() to extract a part of a string. You can specify both the starting
position and the length of the substring.
● Extracting from a Specific Position to the End: If you specify the starting position
only, the substring will include all characters from that position to the end of the string.
● Extracting the Entire String: If you omit both parameters, you get a copy of the entire
string. Not much of a substring!
● Position Beyond String Length: If the starting position exceeds the length of the string,
an empty string is returned.
CS 100 Computational Problem Solving
1.3 Practice
fullName.
5. Extract the lastName from fullName.
CS 100 Computational Problem Solving
1.4 Summary
String Operations
CS 100 Computational Problem Solving
2. Operators
2.1 Objectives
Arithmetic operators allow you to perform basic mathematical operations such as addition,
subtraction, multiplication, division, and modulus (remainder).
Increment and decrement operators increase or decrease a value by one. The prefix form changes
the value before the operation, while the postfix form changes it afterward.
CS 100 Computational Problem Solving
Assignment operators are used to assign values to variables, but they can also perform arithmetic
while assigning the result back to the variable.
Relational operators compare two values and return a boolean value (true or false). They are
commonly used in conditional statements.
● == : Equal to
● != : Not equal to
● > : Greater than
● < : Less than
● >= : Greater than or equal to
● <= : Less than or equal to
6. Right Shift (>>): Shifts the bits of the number to the right, discarding bits shifted off and
filling with 0s on the left.
The ternary operator is a shorthand for if-else statements. The condition is evaluated first, and if
it returns true, the first expression is executed, and if it returns false, the second expression is
executed. The syntax of the ternary operator is as follows:
CS 100 Computational Problem Solving
Unary operators act on a single operand. Examples include unary plus (+), minus (-), increment
(++), and decrement (--).
The sizeof operator returns the size (in bytes) of a data type or variable.
Operators in C++ follow a specific precedence that determines the order in which operations are
performed. For example, multiplication and division have higher precedence than addition and
subtraction.
Associativity defines the direction in which an expression is evaluated when operators have the
same precedence (left-to-right or right-to-left).
Punctuators such as parentheses (), braces {}, semicolons ;, and brackets [] play a significant role
in grouping and terminating expressions or functions.
Example:
1. Arithmetic Operations
Write a program that performs addition, subtraction, multiplication, division, and
modulus on two integers.
2. Logical and Relational Operators
Write a program that compares two integers using relational and logical operators, then
outputs the result.
3. Bitwise Operations
Write a program that uses bitwise shifting to multiply and divide a number by powers of
2.
Practice Question
Task 1:
● Print the final values of a, b, and c after the operation, along with the result.
CS 100 Computational Problem Solving
Explanation:
● Write the following expression that combines pre-increment and post-increment with the
logical AND (&&) operator.
● Print the final values of x and y along with the result of the condition.
Explanation:
Since both conditions (++x > 2 and y++ < 5) are true, the overall result of the condition is
true.
CS 100 Computational Problem Solving
● Be careful not to confuse logical (&&, ||) and bitwise (&, |) operators.
● Use parentheses to clarify complex expressions and avoid unintended precedence issues.
● Avoid overusing the ternary operator, as it can reduce code readability in complex
cases.