SlideShare a Scribd company logo
1 of 29
CS 103 1
Strings in C++
The string Class
• Definition of Strings
• How to declare strings in C++: the string class
• Operations on strings
– Concatenation, comparison operators, and [ ]
• Functions of the string class
– length, size, empty,
– insert, substr, replace, erase, clear, find
• Useful char functions in the C library <ctype.h>
CS 103 2
Definition of Strings
• Generally speaking, a string is a sequence
of characters
• Examples: “hello”, “high school”, “H2O”.
• Typical desirable operations on strings are:
– Concatenation: “high”+“school”=“highschool”
– Comparisons: “high”<“school” // alphabetical
– Finding/retrieving/modifying/deleting/inserting
substrings in a given string
CS 103 3
Strings in C
• In C, a string can be a specially terminated char array or
char pointer
– a char array, such as char str[ ]=“high”;
– a char pointer, such as char *p = “high”;
• If a char array, the last element of the array must be equal
to ‘0’, signaling the end
• For example, the above str[] is really of length 5:
str[0]=‘h’ str[1]=‘i’ str[2]=‘g’ str[3]=‘h’ str[4]=‘0’
• The same array could’ve been declared as:
– char str[5] = {‘h’,’i’, ‘g’,’h’,’0’};
• If you write char str[4] = {‘h’,’i’, ‘g’,’h’};, then str is an
array of chars but not a string.
• In char *p=“high”; the system allocates memory of 5
characters long, stores “high” in the first 4, and ‘0’ in the
5th.
CS 103 4
The string Class in C++
• C++ has a <string> library
• Include it in your programs when you wish
to use strings: #include <string>
• In this library, a class string is defined and
implemented
• It is very convenient and makes string
processing easier than in C
CS 103 5
Declaration of strings
• The following instructions are all equivalent. They
declare x to be an object of type string, and assign
the string “high school” to it:
– string x(“high school”);
– string x= “high school”;
– string x; x=“high school”;
CS 103 6
Operations on strings
(Concatenation)
• Let x and y be two strings
• To concatenate x and y, write: x+y
string x= “high”;
string y= “school”;
string z;
z=x+y;
cout<<“z=“<<z<<endl;
z =z+“ was fun”;
cout<<“z=“<<z<<endl;
Output:
z=highschool
z= highschool was fun
CS 103 7
Concatenation of Mixed-Style
Strings
• In where s is of type string,
– u can be
A string object, or
a C-style string (a char array or a char pointer),
a C-style char
or a double-quoted string,
or a single-quoted character.
– Same with v and w.
– At least u or v or w must be a string object
s=u+v+w;
CS 103 8
Example of Mixed-Style Concat
string x= “high”;
char y[]= “school”;
char z[]= {‘w’,’a’,’s’,’0’};
char *p = “good”;
string s= x+y+’ ‘+z+” very”+” “+p+’!’;
cout<<“s=“<<s<<endl;
cout<<“s=“+s<<endl;
Output:
s=highschool was very good!
s=highschool was very good!
CS 103 9
The concat-assign Operator +=
• Assume x is a string object.
• The statement
x += y;
is equivalent to
x=x+y;
where y can be a string object, a C-style
string variable, a char variable, a double-
quoted string, or a single-quoted char.
CS 103 10
Comparison Operators for
string Objects
• We can compare two strings x and y using the
following operators: ==, !=, <, <=, >, >=
• The comparison is alphabetical
• The outcome of each comparison is: true or false
• The comparison works as long as at least x or y is
a string object. The other string can be a string
object, a C-style string variable, or a double-
quoted string.
CS 103 11
Example of String Comparisons
string x= “high”;
char y[]= “school”;
char *p = “good”;
If (x<y)
cout<<“x<y”<<endl;
If (x<“tree”)
cout<<“x<tree”<,endl;
If (“low” != x)
cout<<“low != x”<<endl;
if( (p>x)
cout<<“p>x”<<endl;
Else
cout<<“p<=x”<<endl;
Output:
x<y
x<tree
low != x
p>x
CS 103 12
The Index Operator []
• If x is a string object, and you wish to
obtain the value of the k-th character in the
string, you write: x[k];
• This feature makes string objects appear
like arrays of chars.
string x= “high”;
char c=x[0]; // c is ‘h’
c=x[1]; // c is ‘i’
c=x[2]; // c is g
CS 103 13
Getting a string Object Length
& Checking for Emptiness
• To obtain the length of a string object x,
call the method length() or size():
• To check of x is empty (that is, has no
characters in it):
int len=x.length( );
--or--
int len=x.size( );
bool x.empty();
CS 103 14
Obtaining Substrings of Strings
• Logically, a substring of a string x is a
subsequence of consecutive characters in x
• For example, “rod” is a substring of “product”
• If x is a string object, and we want the substring
that begins at position pos and has len characters
(where pos and len are of type int), write:
• The default value of len is x.length( )
• The default value for pos is 0
string y = x.substr(pos,len);
string y = x.substr(pos);//x[pos..end-1]
CS 103 15
Inserting a String Inside Another
• Suppose x is a string object, and let y be
another string to be inserted at position pos
of the string of x
• To insert y, do:
• The argument y can be: a string object, a C-
style string variable, or a double-quoted
string
x.insert(pos,y);
CS 103 16
Replacing a Substring by
Another
• Suppose x is a string object, and suppose
you want to replace the characters in the
range [pos,pos+len) in x by a string y.
• To do so, write:
• The argument y can be: a string object, a C-
style string variable, or a double-quoted
string
x.replace(pos,len,y);
CS 103 17
Deleting (Erasing) a Substring
of a string Object
• Suppose x is a string object, and suppose
you want to delete/erase the characters in
the range [pos,pos+len) in x.
• To do so, write:
• The default value of len is the x.length( )
• The default value for pos is 0
• To erase the whole string of x, do:
x.erase(pos,len);
x.clear( );
x.erase(pos); // erases x[pos..end-1]
CS 103 18
Searching for (and Finding)
Patterns in Strings
• Suppose x is a string object, and suppose
you want to search for a string y in x.
• To do so, write:
• This method returns the starting index of the
leftmost occurrence of y in x, if any
occurrence exits; otherwise, the method
returns the length of x.
• To search starting from a position pos, do
int startLoc = x.find(y);
int startLoc = x.find(y, pos);
CS 103 19
Searching for Patterns (Contd.)
• To search for the rightmost occurrence of y
in x, do
• In all the versions of find and rfind, the
argument y can be a string object, a C-style
string variable, double-quoted string, a char
variable, or a single-quoted char.
startLoc = x.rfind(y); // or
startLoc = x.rfind(y, pos);
CS 103 20
An Example
string x=“FROM:ayoussef@gwu.edu”;
int colonPos=x.find(‘:’);
string prefix=x.substr(0,colonPos); //=FROM
string suffix = x. substr(colonPos+1);
cout<<“-This message is from ”<<suffix<<endl;
Output:
-This message is from ayoussef@gwu.edu
CS 103 21
Another Example
#include <fstream>
// this code segment reads lines from an input file and
// appends them to a string object called body. It does
// so until it sees a line equal to ##########
ifstream in(“inputFileName.txt”);
string body; char line[1000];
while(in.getline(line,1000)){ //puts in line the next line w/o n
string lineString (line);
if (lineString != "##########")
body += lineString +'n';// appends line to body
else
break; // exits the while loop
}
CS 103 22
Trimming Leading & Trailing Spaces
// this function removes leading and trailing spaces from x
void trim (string& x){
int k = 0; // k will procced to the first non-blank char
while(k<x.size() &&(x[k]==' ' || x[k]=='t' || x[k]=='n'))
k++;
x. erase(0,k);
int s=x.size();
// s will move backward to the rightmost non-blank char
while(s>0 &&(x[s-1]==' ' || x[s-1]=='t' || x[s-1]=='n'))
s--;
x.erase(s);
}
CS 103 23
What if You Want to Use C-Style
Strings
• You can!
• C has a library <strings.h> which provides several
string processing functions
• Some of the more commonly used functions in
that library are presented in the next two slides
• In those functions, most of the arguments are of
type char *str. That can be replaced by char str[];
CS 103 24
C Library <strings.h> for String
Operations
• char *strcpy(char *dst, char *src);
– Copies the string src to string dest
• char *strncpy(char *dst, char *src, int n);
– Copies the first n characters of src to dest
• char * strcat(*dst, char *src);
– Concatenate src to the end of dst.
• char * strcat(*dst, char *src, int n);
– Concatenate first n chars of src to end of dst.
CS 103 25
• int strcmp(char *str1, char *str2);
– Returns 0 if str1=str2, negative if str1<str2, positive if str1>str2
• int strncmp(char *str1, char *str2, int n);
– Same as strcmp except it considers the first n chars of each string
• int strlen(char *str); // returns the length of str
• char * strchr(char *str, int c);
– Returns a char pointer to the 1st occurrence of character c in str, or
NULL otherwise.
• char * strstr(char *str, char *pat);
– Returns a char pointer to the 1st occurrence of string pat in str, or
NULL otherwise.
• Plus some other commands
• Remarks:
– in strcpy, strncpy, strcat, and strncat, make sure that the dst string has
enough space to accommodate the string copied or cancatenated to it
– If the strings are arrays, also make sure that the array dst is large enough
to to accommodate the string copied or cancatenated to it
CS 103 26
Correspondence between the C
library and the C++ string Class
C Library Functions C++ string operators/methods
strcpy = (the assignment operator)
strcat += (assign+concat operator)
strcmp = =, !=, <, >, <=, >=
strchr, strstr
strrchr
.find( ) method
.rfind( ) method
strlen .size( ) or .length( ) methods
CS 103 27
Char Functions in C (and C++)
• The <ctype.h> library in C provides useful
functions for single char variables
• The next slide gives the most common char
functions.
• Although the input argument appears to be
of type int, it is actually a char.
CS 103 28
• int isalnum(int c); //non-zero iff c is alphanumeric
• int isalpha(int c); //non-zero iff c is alphabetic
• int isdigit(int c); //non-zero iff c a digit: 0 to 9
• int islower(int c); //non-zero iff c is lower case
• int ispunct(int c); //non-zero iff c is punctuation
• int isspace(int c); //non-zero iff c is a space char
• int isupper(int c); // non-zero iff c is upper case
• int isxdigit(int c); //non-zero iff c is hexadecimal
• int tolower(int c); //returns c in lower case
• int toupper(int c); //returns c in upper case
CS 103 29
An example of Using char Functions
//PRECONDITION: str a string object
//POSTCONDITION: every lower-case alphabetical letter
//in str is replaced by its upper-case counterpart
void toupper(string& str){
for(int i=0;i<str.size();i++){
char c=str[i];
if (islower(c)){
char C = toupper(c);
string strC; strC=C;
str.replace(i,1,strC);
}
}
}

More Related Content

Similar to lecture5.ppt

Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its functionFrankie Jones
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxJStalinAsstProfessor
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxAbhimanyuChaure
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 
Strings in c
Strings in cStrings in c
Strings in cvampugani
 
CS101- Introduction to Computing- Lecture 38
CS101- Introduction to Computing- Lecture 38CS101- Introduction to Computing- Lecture 38
CS101- Introduction to Computing- Lecture 38Bilal Ahmed
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentationAliul Kadir Akib
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsJamesChristianGadian
 
Finding similar items in high dimensional spaces locality sensitive hashing
Finding similar items in high dimensional spaces  locality sensitive hashingFinding similar items in high dimensional spaces  locality sensitive hashing
Finding similar items in high dimensional spaces locality sensitive hashingDmitriy Selivanov
 
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...Mail.ru Group
 

Similar to lecture5.ppt (20)

Strings
StringsStrings
Strings
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
Python Cheat Sheet
Python Cheat SheetPython Cheat Sheet
Python Cheat Sheet
 
Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 
Strings
StringsStrings
Strings
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
 
Unitii string
Unitii stringUnitii string
Unitii string
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
Strings
StringsStrings
Strings
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Strings in c
Strings in cStrings in c
Strings in c
 
CS101- Introduction to Computing- Lecture 38
CS101- Introduction to Computing- Lecture 38CS101- Introduction to Computing- Lecture 38
CS101- Introduction to Computing- Lecture 38
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
Strings1
Strings1Strings1
Strings1
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
 
Finding similar items in high dimensional spaces locality sensitive hashing
Finding similar items in high dimensional spaces  locality sensitive hashingFinding similar items in high dimensional spaces  locality sensitive hashing
Finding similar items in high dimensional spaces locality sensitive hashing
 
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
Дмитрий Селиванов, OK.RU. Finding Similar Items in high-dimensional spaces: L...
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 

Recently uploaded

NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...Boston Institute of Analytics
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfBoston Institute of Analytics
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一F La
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理e4aez8ss
 

Recently uploaded (20)

NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
 

lecture5.ppt

  • 1. CS 103 1 Strings in C++ The string Class • Definition of Strings • How to declare strings in C++: the string class • Operations on strings – Concatenation, comparison operators, and [ ] • Functions of the string class – length, size, empty, – insert, substr, replace, erase, clear, find • Useful char functions in the C library <ctype.h>
  • 2. CS 103 2 Definition of Strings • Generally speaking, a string is a sequence of characters • Examples: “hello”, “high school”, “H2O”. • Typical desirable operations on strings are: – Concatenation: “high”+“school”=“highschool” – Comparisons: “high”<“school” // alphabetical – Finding/retrieving/modifying/deleting/inserting substrings in a given string
  • 3. CS 103 3 Strings in C • In C, a string can be a specially terminated char array or char pointer – a char array, such as char str[ ]=“high”; – a char pointer, such as char *p = “high”; • If a char array, the last element of the array must be equal to ‘0’, signaling the end • For example, the above str[] is really of length 5: str[0]=‘h’ str[1]=‘i’ str[2]=‘g’ str[3]=‘h’ str[4]=‘0’ • The same array could’ve been declared as: – char str[5] = {‘h’,’i’, ‘g’,’h’,’0’}; • If you write char str[4] = {‘h’,’i’, ‘g’,’h’};, then str is an array of chars but not a string. • In char *p=“high”; the system allocates memory of 5 characters long, stores “high” in the first 4, and ‘0’ in the 5th.
  • 4. CS 103 4 The string Class in C++ • C++ has a <string> library • Include it in your programs when you wish to use strings: #include <string> • In this library, a class string is defined and implemented • It is very convenient and makes string processing easier than in C
  • 5. CS 103 5 Declaration of strings • The following instructions are all equivalent. They declare x to be an object of type string, and assign the string “high school” to it: – string x(“high school”); – string x= “high school”; – string x; x=“high school”;
  • 6. CS 103 6 Operations on strings (Concatenation) • Let x and y be two strings • To concatenate x and y, write: x+y string x= “high”; string y= “school”; string z; z=x+y; cout<<“z=“<<z<<endl; z =z+“ was fun”; cout<<“z=“<<z<<endl; Output: z=highschool z= highschool was fun
  • 7. CS 103 7 Concatenation of Mixed-Style Strings • In where s is of type string, – u can be A string object, or a C-style string (a char array or a char pointer), a C-style char or a double-quoted string, or a single-quoted character. – Same with v and w. – At least u or v or w must be a string object s=u+v+w;
  • 8. CS 103 8 Example of Mixed-Style Concat string x= “high”; char y[]= “school”; char z[]= {‘w’,’a’,’s’,’0’}; char *p = “good”; string s= x+y+’ ‘+z+” very”+” “+p+’!’; cout<<“s=“<<s<<endl; cout<<“s=“+s<<endl; Output: s=highschool was very good! s=highschool was very good!
  • 9. CS 103 9 The concat-assign Operator += • Assume x is a string object. • The statement x += y; is equivalent to x=x+y; where y can be a string object, a C-style string variable, a char variable, a double- quoted string, or a single-quoted char.
  • 10. CS 103 10 Comparison Operators for string Objects • We can compare two strings x and y using the following operators: ==, !=, <, <=, >, >= • The comparison is alphabetical • The outcome of each comparison is: true or false • The comparison works as long as at least x or y is a string object. The other string can be a string object, a C-style string variable, or a double- quoted string.
  • 11. CS 103 11 Example of String Comparisons string x= “high”; char y[]= “school”; char *p = “good”; If (x<y) cout<<“x<y”<<endl; If (x<“tree”) cout<<“x<tree”<,endl; If (“low” != x) cout<<“low != x”<<endl; if( (p>x) cout<<“p>x”<<endl; Else cout<<“p<=x”<<endl; Output: x<y x<tree low != x p>x
  • 12. CS 103 12 The Index Operator [] • If x is a string object, and you wish to obtain the value of the k-th character in the string, you write: x[k]; • This feature makes string objects appear like arrays of chars. string x= “high”; char c=x[0]; // c is ‘h’ c=x[1]; // c is ‘i’ c=x[2]; // c is g
  • 13. CS 103 13 Getting a string Object Length & Checking for Emptiness • To obtain the length of a string object x, call the method length() or size(): • To check of x is empty (that is, has no characters in it): int len=x.length( ); --or-- int len=x.size( ); bool x.empty();
  • 14. CS 103 14 Obtaining Substrings of Strings • Logically, a substring of a string x is a subsequence of consecutive characters in x • For example, “rod” is a substring of “product” • If x is a string object, and we want the substring that begins at position pos and has len characters (where pos and len are of type int), write: • The default value of len is x.length( ) • The default value for pos is 0 string y = x.substr(pos,len); string y = x.substr(pos);//x[pos..end-1]
  • 15. CS 103 15 Inserting a String Inside Another • Suppose x is a string object, and let y be another string to be inserted at position pos of the string of x • To insert y, do: • The argument y can be: a string object, a C- style string variable, or a double-quoted string x.insert(pos,y);
  • 16. CS 103 16 Replacing a Substring by Another • Suppose x is a string object, and suppose you want to replace the characters in the range [pos,pos+len) in x by a string y. • To do so, write: • The argument y can be: a string object, a C- style string variable, or a double-quoted string x.replace(pos,len,y);
  • 17. CS 103 17 Deleting (Erasing) a Substring of a string Object • Suppose x is a string object, and suppose you want to delete/erase the characters in the range [pos,pos+len) in x. • To do so, write: • The default value of len is the x.length( ) • The default value for pos is 0 • To erase the whole string of x, do: x.erase(pos,len); x.clear( ); x.erase(pos); // erases x[pos..end-1]
  • 18. CS 103 18 Searching for (and Finding) Patterns in Strings • Suppose x is a string object, and suppose you want to search for a string y in x. • To do so, write: • This method returns the starting index of the leftmost occurrence of y in x, if any occurrence exits; otherwise, the method returns the length of x. • To search starting from a position pos, do int startLoc = x.find(y); int startLoc = x.find(y, pos);
  • 19. CS 103 19 Searching for Patterns (Contd.) • To search for the rightmost occurrence of y in x, do • In all the versions of find and rfind, the argument y can be a string object, a C-style string variable, double-quoted string, a char variable, or a single-quoted char. startLoc = x.rfind(y); // or startLoc = x.rfind(y, pos);
  • 20. CS 103 20 An Example string x=“FROM:ayoussef@gwu.edu”; int colonPos=x.find(‘:’); string prefix=x.substr(0,colonPos); //=FROM string suffix = x. substr(colonPos+1); cout<<“-This message is from ”<<suffix<<endl; Output: -This message is from ayoussef@gwu.edu
  • 21. CS 103 21 Another Example #include <fstream> // this code segment reads lines from an input file and // appends them to a string object called body. It does // so until it sees a line equal to ########## ifstream in(“inputFileName.txt”); string body; char line[1000]; while(in.getline(line,1000)){ //puts in line the next line w/o n string lineString (line); if (lineString != "##########") body += lineString +'n';// appends line to body else break; // exits the while loop }
  • 22. CS 103 22 Trimming Leading & Trailing Spaces // this function removes leading and trailing spaces from x void trim (string& x){ int k = 0; // k will procced to the first non-blank char while(k<x.size() &&(x[k]==' ' || x[k]=='t' || x[k]=='n')) k++; x. erase(0,k); int s=x.size(); // s will move backward to the rightmost non-blank char while(s>0 &&(x[s-1]==' ' || x[s-1]=='t' || x[s-1]=='n')) s--; x.erase(s); }
  • 23. CS 103 23 What if You Want to Use C-Style Strings • You can! • C has a library <strings.h> which provides several string processing functions • Some of the more commonly used functions in that library are presented in the next two slides • In those functions, most of the arguments are of type char *str. That can be replaced by char str[];
  • 24. CS 103 24 C Library <strings.h> for String Operations • char *strcpy(char *dst, char *src); – Copies the string src to string dest • char *strncpy(char *dst, char *src, int n); – Copies the first n characters of src to dest • char * strcat(*dst, char *src); – Concatenate src to the end of dst. • char * strcat(*dst, char *src, int n); – Concatenate first n chars of src to end of dst.
  • 25. CS 103 25 • int strcmp(char *str1, char *str2); – Returns 0 if str1=str2, negative if str1<str2, positive if str1>str2 • int strncmp(char *str1, char *str2, int n); – Same as strcmp except it considers the first n chars of each string • int strlen(char *str); // returns the length of str • char * strchr(char *str, int c); – Returns a char pointer to the 1st occurrence of character c in str, or NULL otherwise. • char * strstr(char *str, char *pat); – Returns a char pointer to the 1st occurrence of string pat in str, or NULL otherwise. • Plus some other commands • Remarks: – in strcpy, strncpy, strcat, and strncat, make sure that the dst string has enough space to accommodate the string copied or cancatenated to it – If the strings are arrays, also make sure that the array dst is large enough to to accommodate the string copied or cancatenated to it
  • 26. CS 103 26 Correspondence between the C library and the C++ string Class C Library Functions C++ string operators/methods strcpy = (the assignment operator) strcat += (assign+concat operator) strcmp = =, !=, <, >, <=, >= strchr, strstr strrchr .find( ) method .rfind( ) method strlen .size( ) or .length( ) methods
  • 27. CS 103 27 Char Functions in C (and C++) • The <ctype.h> library in C provides useful functions for single char variables • The next slide gives the most common char functions. • Although the input argument appears to be of type int, it is actually a char.
  • 28. CS 103 28 • int isalnum(int c); //non-zero iff c is alphanumeric • int isalpha(int c); //non-zero iff c is alphabetic • int isdigit(int c); //non-zero iff c a digit: 0 to 9 • int islower(int c); //non-zero iff c is lower case • int ispunct(int c); //non-zero iff c is punctuation • int isspace(int c); //non-zero iff c is a space char • int isupper(int c); // non-zero iff c is upper case • int isxdigit(int c); //non-zero iff c is hexadecimal • int tolower(int c); //returns c in lower case • int toupper(int c); //returns c in upper case
  • 29. CS 103 29 An example of Using char Functions //PRECONDITION: str a string object //POSTCONDITION: every lower-case alphabetical letter //in str is replaced by its upper-case counterpart void toupper(string& str){ for(int i=0;i<str.size();i++){ char c=str[i]; if (islower(c)){ char C = toupper(c); string strC; strC=C; str.replace(i,1,strC); } } }