SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Strings
(CS1123)
By
Dr. Muhammad Aleem,
Department of Computer Science,
Mohammad Ali Jinnah University, Islamabad
Reading strings (C-String) with spaces
• getline function of input-stream (cin) can be
used to get input in a character array including
spaces.
• Syntax:
cin.getline(char[ ] arr, int size);
Character Array Size of Array
Reading strings (C-String) with spaces
- OR, the programmer can specify its own
terminating character
• Syntax:
cin.getline(char[ ] arr, int size, char term);
Character Array Array size Terminating
character
Reading strings (C-String) with spaces
• Examples
char line[25];
cout << "Type a line terminated by enter key”;
cin.getline(line,25);
cout << “You entered: ” << line;
• Reads up to 24 characters (char type values) and
inserts ‘0’ (Null character) at the end. If extra
characters (> 25 in this example) entered by user,
C++ ignore the remaining characters.
Reading strings (C-String) with spaces
char name[60];
char university[100];
cout << "Enter your name: ";
cin.getline(name,60);
cout << "Enter your university name: ";
cin.getline(university,100);
cout << name << “ study in ”<< university;
Reading strings (C-String) with spaces
char line[100];
cout << "Type a line terminated by t: “;
cin.getline( line, 100, 't' );
cout << line;
Strings - Introduction
• A string is a sequence of characters.
• We have used null terminated <char> arrays (C-
strings) to store and manipulate strings.
• C++ also provides a class called string
• We must include <string> in our program.
– More convenient than the C-String
Operations
• Creating strings.
• Reading strings from keyboard.
• Displaying strings to the screen.
• Finding a substring from a string.
• Modifying string.
• Adding strings.
• Accessing characters in a string.
• Obtaining the size of string.
• And many more…
Declaration of strings
• Following instructions are all equivalent.
• They declare country to be an variable of type
string, and assign the string “Pakistan” to it:
–string country(“Pakistan”);
–string country = “Pakistan”;
–string country;
x=“Pakistan”;
Operations: Concatenation
• Concatenation: combining two or ore strings into a
single string
• 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
concat-assign Operator +=
• Assume x is a string.
• The statement
x += y;
is equivalent to
x = x + y;
where y can be a string OR a C-style (character
string), a char variable, a double-quoted string
literal, or a single-quoted char.
Example of Mixed-Style Concat.
string x= “high”;
char y[ ]= “school”;
char z[ ]= {‘w’, ’a’, ’s’, ‘0’};
string 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!
Example of concat-assign Operator +=
string x = “high”;
string y = “school”;
x+=y;
cout<<“x= “<<x<<endl;
Output:
x= highschool
Comparison Operators for string Objects
• We can compare two strings x and y using the
following operators: ==, !=, <, <=, >, >=
• Comparison is alphabetical, the outcome of each
comparison is: true or false
• Comparison works as long as at least x or y is a
string.
• The other string can be a string Or a C-String
variable, or a double-quoted string (string literal).
Example of String Comparisons
string x = “high”;
char y[ ] = “school”;
if (x<y)
cout<<“x<y”<<endl;
if (x<“tree”)
cout<<“x<tree”<,endl;
if (“low” != x)
cout<<“low != x”<<endl;
Output:
x<y
x<tree
low != x
Using Index Operator [ ]
• If x is a string, and you wish to obtain the value of
the k-th character in the string, you may write:
x[k];
• This feature makes string variables similar to
arrays of char
string x = “high”;
char c = x[0]; // c is ‘h’
c = x[1]; // c is ‘i’
c = x[2]; // c is ‘g’
Getting a string size, checking for Emptiness
• To obtain the size (number of bytes) of a string
variable, call the method length() or size()
functions:
• To check of x is empty (that is, has no characters in
it):
int len = x.length( );
// OR
int len = x.size( );
If (x.empty() )
cout<<“String x is empty…”;
Obtaining sub-strings of Strings
• A substring of a string x is a subsequence of
consecutive characters in x
• Example: “duc” is a substring of “product”
• If x is a string variable, and we want the substring that
begins at position pos and has len characters (where pos
and len are of type int):
• 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( );
Inserting a String Inside Another
• Suppose x is a string, and let y be another string to
be inserted at position pos of the string of x:
• The argument y can be: a string variable, a C-style
string variable, or a double-quoted string:
string str = "to be question";
string str2 = "the ";
str.insert(6,str2); // to be the question
x.insert( pos, y);
Replacing a Substring by Another
• Suppose x is a string variable, and suppose you
want to replace the characters in range
[pos, len] in x by a string y. To do so, write:
• Aargument y can be: a string variable, a C-style
string variable, or a double-quoted string
x.replace(pos, len, y);
Replacing a Substring by Another
• Example:
string base="this is a test string.";
string str2="n example";
string str=base; // "this is a test string."
str.replace(9,5,str2);
cout<<str; // "this is an example string."
Deleting (Erasing) a Substring of a string
• Suppose x is a string variable, and suppose you
want to delete/erase the characters in the range
[pos, len] in x.
• To do so, write:
• The default value of len is the x.length( ):
• To erase the whole string of x, do:
x.erase(pos, len);
x.clear( );
x.erase(pos);
Deleting (Erasing) a Substring of a string
• Example:
string str("This is an example phrase.");
str.erase(11,8);
cout << str << endl;
Output: “This is an phrase.”
Searching for (and Finding) Patterns in Strings
• Suppose x is a string variable, and suppose you
want to search for a string y in x.
• To do so, write:
• 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);
An Example (find and substr)
string x =“FROM:ayoussef@abc.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@abc.edu
Class Exercise
• Write a C++ program that finds number of
occurrences of a string in another string. The program
takes as an input a string str1 (based on string data-
type). Then it ask the user to enter another string
str2. In the end program display the count or number
value stating the occurrence of str2 in str1.

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Strings
StringsStrings
Strings
 
The string class
The string classThe string class
The string class
 
String c
String cString c
String c
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
Strings in c
Strings in cStrings in c
Strings in c
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Strings in C
Strings in CStrings in C
Strings in C
 
Strings
StringsStrings
Strings
 
String in programming language in c or c++
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
 
String in c
String in cString in c
String in c
 
String Handling
String HandlingString Handling
String Handling
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Python strings
Python stringsPython strings
Python strings
 
String java
String javaString java
String java
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 

Andere mochten auch

Analox interview questions and answers
Analox interview questions and answersAnalox interview questions and answers
Analox interview questions and answersjakebrown118
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointersTAlha MAlik
 
Pdhpe presentation
Pdhpe presentationPdhpe presentation
Pdhpe presentationdeana1994
 
Anness publishing interview questions and answers
Anness publishing interview questions and answersAnness publishing interview questions and answers
Anness publishing interview questions and answersjakebrown118
 
Anglian home improvements group interview questions and answers
Anglian home improvements group interview questions and answersAnglian home improvements group interview questions and answers
Anglian home improvements group interview questions and answersjakebrown118
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
Data file handling
Data file handlingData file handling
Data file handlingTAlha MAlik
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structuresTAlha MAlik
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operationsTAlha MAlik
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_ifTAlha MAlik
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 
Cs1123 4 variables_constants
Cs1123 4 variables_constantsCs1123 4 variables_constants
Cs1123 4 variables_constantsTAlha MAlik
 
Cs1123 2 comp_prog
Cs1123 2 comp_progCs1123 2 comp_prog
Cs1123 2 comp_progTAlha MAlik
 

Andere mochten auch (15)

Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
Cs1123 1 intro
Cs1123 1 introCs1123 1 intro
Cs1123 1 intro
 
Analox interview questions and answers
Analox interview questions and answersAnalox interview questions and answers
Analox interview questions and answers
 
Cs1123 11 pointers
Cs1123 11 pointersCs1123 11 pointers
Cs1123 11 pointers
 
Pdhpe presentation
Pdhpe presentationPdhpe presentation
Pdhpe presentation
 
Anness publishing interview questions and answers
Anness publishing interview questions and answersAnness publishing interview questions and answers
Anness publishing interview questions and answers
 
Anglian home improvements group interview questions and answers
Anglian home improvements group interview questions and answersAnglian home improvements group interview questions and answers
Anglian home improvements group interview questions and answers
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
Data file handling
Data file handlingData file handling
Data file handling
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
Cs1123 5 selection_if
Cs1123 5 selection_ifCs1123 5 selection_if
Cs1123 5 selection_if
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Cs1123 4 variables_constants
Cs1123 4 variables_constantsCs1123 4 variables_constants
Cs1123 4 variables_constants
 
Cs1123 2 comp_prog
Cs1123 2 comp_progCs1123 2 comp_prog
Cs1123 2 comp_prog
 

Ähnlich wie Cs1123 9 strings

Ähnlich wie Cs1123 9 strings (20)

lecture5.ppt
lecture5.pptlecture5.ppt
lecture5.ppt
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
String in programming language in c or c++
String in programming language in c or c++String in programming language in c or c++
String in programming language in c or c++
 
Python data handling
Python data handlingPython data handling
Python data handling
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Strings
StringsStrings
Strings
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
Matlab strings
Matlab stringsMatlab strings
Matlab strings
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Computer programming 2 Lesson 11
Computer programming 2  Lesson 11Computer programming 2  Lesson 11
Computer programming 2 Lesson 11
 
Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
stringsinpython-181122100212.pdf
stringsinpython-181122100212.pdfstringsinpython-181122100212.pdf
stringsinpython-181122100212.pdf
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Strings
StringsStrings
Strings
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 

Kürzlich hochgeladen

Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...ritikasharma
 
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur Escorts
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur EscortsCall Girl Nagpur Roshni Call 7001035870 Meet With Nagpur Escorts
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
👙 Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Serviceanamikaraghav4
 
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...SofiyaSharma5
 
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...noor ahmed
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...russian goa call girl and escorts service
 
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...noor ahmed
 
College Call Girls New Alipore - For 7001035870 Cheap & Best with original Ph...
College Call Girls New Alipore - For 7001035870 Cheap & Best with original Ph...College Call Girls New Alipore - For 7001035870 Cheap & Best with original Ph...
College Call Girls New Alipore - For 7001035870 Cheap & Best with original Ph...anamikaraghav4
 
Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...
Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...
Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...noor ahmed
 
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
👙 Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Serviceanamikaraghav4
 
👙 Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Serviceanamikaraghav4
 
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...Apsara Of India
 
Call Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
Call Girls Nashik Gayatri 7001305949 Independent Escort Service NashikCall Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
Call Girls Nashik Gayatri 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingNitya salvi
 
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...rahim quresi
 
Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448ont65320
 

Kürzlich hochgeladen (20)

Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
 
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur Escorts
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur EscortsCall Girl Nagpur Roshni Call 7001035870 Meet With Nagpur Escorts
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur Escorts
 
👙 Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Sonagachi 💫💫7001035870 Model escorts Service
 
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...
Low Rate Young Call Girls in Surajpur Greater Noida ✔️☆9289244007✔️☆ Female E...
 
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
 
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
 
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Behala ⟟ 8250192130 ⟟ High Class Call Girl In...
 
College Call Girls New Alipore - For 7001035870 Cheap & Best with original Ph...
College Call Girls New Alipore - For 7001035870 Cheap & Best with original Ph...College Call Girls New Alipore - For 7001035870 Cheap & Best with original Ph...
College Call Girls New Alipore - For 7001035870 Cheap & Best with original Ph...
 
Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...
Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...
Book Paid Sonagachi Call Girls Kolkata 𖠋 8250192130 𖠋Low Budget Full Independ...
 
Goa Call "Girls Service 9316020077 Call "Girls in Goa
Goa Call "Girls  Service   9316020077 Call "Girls in GoaGoa Call "Girls  Service   9316020077 Call "Girls in Goa
Goa Call "Girls Service 9316020077 Call "Girls in Goa
 
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
 
👙 Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Shyam Bazar 💫💫7001035870 Model escorts Service
 
👙 Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service
 
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
 
Russian ℂall gIRLS In Goa 9316020077 ℂall gIRLS Service In Goa
Russian ℂall gIRLS In Goa 9316020077  ℂall gIRLS Service  In GoaRussian ℂall gIRLS In Goa 9316020077  ℂall gIRLS Service  In Goa
Russian ℂall gIRLS In Goa 9316020077 ℂall gIRLS Service In Goa
 
Call Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
Call Girls Nashik Gayatri 7001305949 Independent Escort Service NashikCall Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
Call Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
 
Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448Beautiful 😋 Call girls in Lahore 03210033448
Beautiful 😋 Call girls in Lahore 03210033448
 

Cs1123 9 strings

  • 1. Strings (CS1123) By Dr. Muhammad Aleem, Department of Computer Science, Mohammad Ali Jinnah University, Islamabad
  • 2. Reading strings (C-String) with spaces • getline function of input-stream (cin) can be used to get input in a character array including spaces. • Syntax: cin.getline(char[ ] arr, int size); Character Array Size of Array
  • 3. Reading strings (C-String) with spaces - OR, the programmer can specify its own terminating character • Syntax: cin.getline(char[ ] arr, int size, char term); Character Array Array size Terminating character
  • 4. Reading strings (C-String) with spaces • Examples char line[25]; cout << "Type a line terminated by enter key”; cin.getline(line,25); cout << “You entered: ” << line; • Reads up to 24 characters (char type values) and inserts ‘0’ (Null character) at the end. If extra characters (> 25 in this example) entered by user, C++ ignore the remaining characters.
  • 5. Reading strings (C-String) with spaces char name[60]; char university[100]; cout << "Enter your name: "; cin.getline(name,60); cout << "Enter your university name: "; cin.getline(university,100); cout << name << “ study in ”<< university;
  • 6. Reading strings (C-String) with spaces char line[100]; cout << "Type a line terminated by t: “; cin.getline( line, 100, 't' ); cout << line;
  • 7. Strings - Introduction • A string is a sequence of characters. • We have used null terminated <char> arrays (C- strings) to store and manipulate strings. • C++ also provides a class called string • We must include <string> in our program. – More convenient than the C-String
  • 8. Operations • Creating strings. • Reading strings from keyboard. • Displaying strings to the screen. • Finding a substring from a string. • Modifying string. • Adding strings. • Accessing characters in a string. • Obtaining the size of string. • And many more…
  • 9. Declaration of strings • Following instructions are all equivalent. • They declare country to be an variable of type string, and assign the string “Pakistan” to it: –string country(“Pakistan”); –string country = “Pakistan”; –string country; x=“Pakistan”;
  • 10. Operations: Concatenation • Concatenation: combining two or ore strings into a single string • 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
  • 11. concat-assign Operator += • Assume x is a string. • The statement x += y; is equivalent to x = x + y; where y can be a string OR a C-style (character string), a char variable, a double-quoted string literal, or a single-quoted char.
  • 12. Example of Mixed-Style Concat. string x= “high”; char y[ ]= “school”; char z[ ]= {‘w’, ’a’, ’s’, ‘0’}; string 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!
  • 13. Example of concat-assign Operator += string x = “high”; string y = “school”; x+=y; cout<<“x= “<<x<<endl; Output: x= highschool
  • 14. Comparison Operators for string Objects • We can compare two strings x and y using the following operators: ==, !=, <, <=, >, >= • Comparison is alphabetical, the outcome of each comparison is: true or false • Comparison works as long as at least x or y is a string. • The other string can be a string Or a C-String variable, or a double-quoted string (string literal).
  • 15. Example of String Comparisons string x = “high”; char y[ ] = “school”; if (x<y) cout<<“x<y”<<endl; if (x<“tree”) cout<<“x<tree”<,endl; if (“low” != x) cout<<“low != x”<<endl; Output: x<y x<tree low != x
  • 16. Using Index Operator [ ] • If x is a string, and you wish to obtain the value of the k-th character in the string, you may write: x[k]; • This feature makes string variables similar to arrays of char string x = “high”; char c = x[0]; // c is ‘h’ c = x[1]; // c is ‘i’ c = x[2]; // c is ‘g’
  • 17. Getting a string size, checking for Emptiness • To obtain the size (number of bytes) of a string variable, call the method length() or size() functions: • To check of x is empty (that is, has no characters in it): int len = x.length( ); // OR int len = x.size( ); If (x.empty() ) cout<<“String x is empty…”;
  • 18. Obtaining sub-strings of Strings • A substring of a string x is a subsequence of consecutive characters in x • Example: “duc” is a substring of “product” • If x is a string variable, and we want the substring that begins at position pos and has len characters (where pos and len are of type int): • 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( );
  • 19. Inserting a String Inside Another • Suppose x is a string, and let y be another string to be inserted at position pos of the string of x: • The argument y can be: a string variable, a C-style string variable, or a double-quoted string: string str = "to be question"; string str2 = "the "; str.insert(6,str2); // to be the question x.insert( pos, y);
  • 20. Replacing a Substring by Another • Suppose x is a string variable, and suppose you want to replace the characters in range [pos, len] in x by a string y. To do so, write: • Aargument y can be: a string variable, a C-style string variable, or a double-quoted string x.replace(pos, len, y);
  • 21. Replacing a Substring by Another • Example: string base="this is a test string."; string str2="n example"; string str=base; // "this is a test string." str.replace(9,5,str2); cout<<str; // "this is an example string."
  • 22. Deleting (Erasing) a Substring of a string • Suppose x is a string variable, and suppose you want to delete/erase the characters in the range [pos, len] in x. • To do so, write: • The default value of len is the x.length( ): • To erase the whole string of x, do: x.erase(pos, len); x.clear( ); x.erase(pos);
  • 23. Deleting (Erasing) a Substring of a string • Example: string str("This is an example phrase."); str.erase(11,8); cout << str << endl; Output: “This is an phrase.”
  • 24. Searching for (and Finding) Patterns in Strings • Suppose x is a string variable, and suppose you want to search for a string y in x. • To do so, write: • 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);
  • 25. An Example (find and substr) string x =“FROM:ayoussef@abc.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@abc.edu
  • 26. Class Exercise • Write a C++ program that finds number of occurrences of a string in another string. The program takes as an input a string str1 (based on string data- type). Then it ask the user to enter another string str2. In the end program display the count or number value stating the occurrence of str2 in str1.