Lecture 10
Lecture 10
Algorithms 1
CHAPTER 4
STATIC DATA STRUCTURE (PART3):
STRING & STRUCT
Sep – Dec 2023
Outline
Strings
Definition
Declaration
Example
Strings in C++
Struct
Definition
Declaration
Example
Struct in C++
STRINGS
CONSTANT String:
It is a sequence of characters enclosed in apostrophes:
Examples:
“Give an integer between 1 and 10:”
“This program gives the Nth term of the FIBO sequence”
“The equation has no solutions”
STRINGS
It is a 1D array of characters:
Sentence H e l l o W o r l d
0 1 2 3 4 5 6 7 8 9 10
A = Title[2]
B = Title[i]
C = Title[i + j]
Title[2] = k
Title[i] = Z
Title[i DIV 2] = V
STRINGS
Remark:
The maximum length of a string is 255.
Anadditional position contains the length of the string.
(The length of the string is stored in a char (Byte, i.e., max
= 255) )
Thedimension of a string is its length, i.e., the number of
characters it comprises. By default, the dimensional
length of a string is 255.
STRINGS
FORMAT:
String my_string
OR
String my_string [max_size]
Example:
String P
READ (P)
IF P[i] = ‘A’ THEN count = count + 1
STRINGS
Standard Functions & Procedures
String Function Copy(String str, Integer P, Integer N)
Description:
This function returns a sub-string of N characters starting from the position
P of the string str
Example:
STR = Copy (“NationalSchool”, 8, 6) //STR = “School”
STRINGS
Standard Functions & Procedures
String Function Concat(Strinf str1, String str2 )
Description:
This function returns the concatenation of two strings str1 and str2
Example:
STR1 = “School”
STR2 = Concat(“National ”, STR1) //STR2 = “National School”
STRINGS
Standard Functions & Procedures
Integer Function Pos(Strinf subStr, String str )
Description:
This function returns the first position of the substring SubStr in the string
str. If the substring subStr does not exists in the string str, the
function returns -1.
Example:
p = Pos(“School”, “National School”) //p = 9
p = Pos(“R”, “Program”) //p = -1
STRINGS
Standard Functions & Procedures
Integer Function Length(String str )
Description:
This function returns the length of the string str.
Example:
N = Length(“National School”) //N = 15
STRINGS
Standard Functions & Procedures
Procedure Delete(Var String str, Integer P, Integer N)
Description:
Delete N characters from str starting from the position P.
If the string contains less than N char from P, delete till the end of the
string.
Example:
String str = “National School”
Delete(str, 8, 7) //str = “National”
STRINGS
Standard Functions & Procedures
Procedure Insert(String str2, Var String str1, Integer P)
Description:
Inserts a string str2 into another string str1 at position P.
Example:
String str1 = “Hi! how are you?”
String str2 = “ Mahmoud”
insert(str2, str1, 2) //str1 = “Hi Mahmoud! how are you?”
STRINGS
Standard Functions & Procedures
Procedure Str(Integer/Real N, Var String str)
Description:
Convert a numerical value N into a string str.
Example:
Str (2012, str) // str = '2012' ;
Str (14.52, str) // str = '1.4520000000E+01’
STRINGS
Standard Functions & Procedures
Procedure Val(String str, Var Integer N, Var Integer Err)
Description:
Convert a string str to a numerical value N. Additionally, it provides an
error code Err indicating whether the operation was carried out
successfully.
Example:
Val ("2021", n, err) // n = 2021 and err = -1;
VAL ("45A6", n, err) // n=0 and err= 2
n is Integer: Val ("3.14", n, err ) // n = 0 and err = 1
n is Real: Val ("3.14", n, err ) // n = 3.14 and err = -1
STRINGS
Application : Palindrome
Example:
Engage le jeu que je le gagne
Name now one man
STRINGS
Application : Palindrome
Modular breakdown:
We are going to build a module that checks if a given string is a
palindrome.
We will also need the standard functions:
Length, the length of our initial string
Upercase, convert everything to uppercase since we do not know if
the characters are in uppercase, lowercase, or a mix of both
Ord: to determine the alphabetical order number of a character in
the ASCII list in order to consider only characters ranging from the
65th to the 90th position ('A', 'B', 'C', ..., 'Z').
STRINGS
Application : Palindrome
Construction of PALIND
Analysis
The basic idea is:
FUNCTION
• To rewrite the original string
String Ph Boolean forwards (Str1) and backward
PALIND (Str2) while removing all
characters other than alphabetic
Return True if Ph is palindrome characters.
• Then, if Str1 equals Str2, the two
strings are identical, indicating a
palindrome.
STRINGS
Application : Palindrome
Assignment
s2 = s1;
Makes a separate copy
s2.assign(s1);
Same as s2 = s1;
myString.assign(s, start, N);
Copies N characters from s, beginning at index start
Individual characters
s2[0] = s3[2];
STRINGS in C++
String Assignment and Concatenation
Range checking
s3.at( index );
Returns character at index
Can throw out_of_range exception
Concatenation
s3.append( "pet" );
s3 += "pet";
Both add "pet" to end of s3
s3.append( s1, start, N );
Appends N characters from s1, beginning at index start
STRINGS in C++
Comparing strings
Overloaded operators
==, !=, <, >, <= and >=
Return bool
s1.compare(s2)
Returns positive if s1 lexicographically greater
Compares letter by letter
'B' lexicographically greater than 'A'
Returns negative if less, zero if equal
s1.compare(start, length, s2, start,
length)
Compare portions of s1 and s2
s1.compare(start, length, s2)
Compare portion of s1 with all of s2
STRINGS in C++
Substrings & Swapping
Function substr gets substring
s1.substr( start, N );
Gets N characters, beginning with index start
Returns substring
s1.swap(s2);
Switch contents of two strings
STRINGS in C++
Characteristics
Member functions
s1.size() and s1.length()
Number of characters in string
s1.capacity()
Number of elements that can be stored without reallocation
s1.max_size()
Maximum possible string size
s1.empty()
Returns true if empty
s1.resize(newlength)
Resizes string to newlength
STRINGS in C++
Finding Strings and Characters in a string
Find functions
If found, index returned
If not found, string::npos returned
Public static constant in class string
s1.find( s2 )
s1.rfind( s2 )
Searches right-to-left
s1.find_first_of( s2 )
Returns first occurrence of any character in s2
s1.find_frist_of( "abcd" )
Returns index of first 'a', 'b', 'c' or 'd'
STRINGS in C++
Finding Strings and Characters in a string
Find functions
s1.find_last_of( s2 )
Finds last occurrence of any character in s2
s1.find_first_not_of( s2 )
Finds first character NOT in s2
s1.find_last_not_of( s2 )
Finds last character NOT in s2
STRINGS in C++
Finding Strings and Characters in a string
s1.erase( start )
Erase from index start to end of string, including start
Replace
s1.replace( begin, N, s2)
begin: index in s1 to start replacing
N: number of characters to replace
s2: replacement string
s1.replace( begin, N, s2, index, num )
index: element in s2 where replacement begins
num: number of elements to use when replacing
s1.insert( index, s2 )
Inserts s2 before position index
s1.insert( index, s2, index2, N );
Inserts substring of s2 before position index
Substring is N characters, starting at index2
STRUCT
STRUCT
Definition
In programming, a struct is a collection of elementary
elements, which may vary in types and are referred to
as “member."
These member are organized under a common
object name.
Each field can be either a simple element or a
structure itself.
The purpose of a struct is to encapsulate a set of
information related to a specific object within a
unified structure.
STRUCT
Example
Student
STRUCT Birthday
Integer day
Integer month
Integer year
END
Variable
Birthday Student_birthday
STRUCT
Initializing a Struct:
MyStruct example = {42, 'A', 3.14}
Accessing:
Use the dot (.) operator to access members.
Integer day = Student_birthday.day
Struct type_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
STRUCT
Declaration in C++
Struct type_name {
• type_name is a name for the member_type1 member_name1;
member_type2 member_name2;
structure type member_type3 member_name3;
.
• object_name can be a set of .
valid identifiers for objects that } object_names;