SlideShare a Scribd company logo
CS111 Lab
Strings
Instructor: Michael Gordon
The String class
 A class can be thought of as new type of
variable that you can create.
 C++ is also called “C with classes”
 We’ve used one kind of class already: the
string class, which is not in C.
 string and its functions are built into C++.
 C had C-strings, which can be found in
the cstring library.
Strings
 The internal construction of a string (or
cstring) is really an array of characters.
 The first character of string s is s[0].
 string s = “Hello”;
cout<<s[4]; //prints the letter o
 To find the length of a string s we can use
the functions .length() or .size()
 int x = s.length(); gives x the value of the
size of the string s (in this case, 5).
Strings
 To “concatenate” two strings s1 and s2:
 string s3 = s1 + s2;
 string s1 = “race”;
 string s2 = “car”;
 string s3 = s1 + s2;
 cout<<s3; //prints racecar
String examples
string first, last, name;
cout<<"Enter your first name:";
cin>>first;
cout<<"Enter your last name:";
cin>>last;
cout<<"Hi, "+first+" "+last;
//concatenation
getline()
 cin reads the typed string only until the first
whitespace.
 If you want the whole string including
those whitespaces, you can use:
 getline(cin, stringname);
 cout<<“Enter your name:”;
 getline(cin, fullname);
 Use fullname instead of first and last.
“dot” functions
 With strings we start dealing with a new
kind of function call. These functions
operate on a specific “object” (an
instance of a class).
 We write the object name (e.g. the
variable name of the string) followed by
period and the function call (no space
before or after the period).
More functions
 s1.find(string s2) – returns the index of the
beginning of s2 in s1.
 s1 = “hello”;
 s1.find(“lo”) – returns 3
 s1.insert(int i, string s2) – inserts s2 into s1 at
index I
 s1.at(int i) – returns the character at index
i in string s1

More Related Content

PDF
Manipulating strings
PPT
Strings
PPTX
String handling
PPTX
String in c programming
PPTX
String java
PDF
Chapter20 class-example-program
PPTX
16 strings-and-text-processing-120712074956-phpapp02
PPTX
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Manipulating strings
Strings
String handling
String in c programming
String java
Chapter20 class-example-program
16 strings-and-text-processing-120712074956-phpapp02
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing

What's hot (20)

ODP
Dive into CPython Bytecode
DOCX
Arrays in VbScript
PPTX
Character array (strings)
PDF
ES6 General Introduction
PPTX
Lecture 2. mte 407
DOC
Huffman coding01
PDF
The Ring programming language version 1.9 book - Part 123 of 210
PPTX
Syntax Comparison of Golang with C and Java - Mindbowser
PPTX
PPTX
Weekends with Competitive Programming
PDF
Presentation template - jupyter2slides
PDF
Tweaking the Base Score: Lucene/Solr Similarities Explained
PDF
Activate 2019: Tweaking the Base Score: Lucene/Solr Similarities Explained
PDF
Rust Synchronization Primitives
PPTX
C++ training day01
PDF
5 2. string processing
PPTX
String (Computer programming and utilization)
PDF
Hey! There's OCaml in my Rust!
Dive into CPython Bytecode
Arrays in VbScript
Character array (strings)
ES6 General Introduction
Lecture 2. mte 407
Huffman coding01
The Ring programming language version 1.9 book - Part 123 of 210
Syntax Comparison of Golang with C and Java - Mindbowser
Weekends with Competitive Programming
Presentation template - jupyter2slides
Tweaking the Base Score: Lucene/Solr Similarities Explained
Activate 2019: Tweaking the Base Score: Lucene/Solr Similarities Explained
Rust Synchronization Primitives
C++ training day01
5 2. string processing
String (Computer programming and utilization)
Hey! There's OCaml in my Rust!
Ad

Viewers also liked (8)

PDF
Strings2
PDF
Arrays, continued
PDF
PDF
Introduction to Computer Science 111 Lab
PDF
While loops
PPT
貧窮
PDF
Millennial white paper
PDF
Strings2
Arrays, continued
Introduction to Computer Science 111 Lab
While loops
貧窮
Millennial white paper
Ad

Similar to Strings1 (20)

PPTX
String in programming language in c or c++
PPTX
lecture-5 string.pptx
PDF
String class and function for b.tech iii year students
PDF
C string _updated_Somesh_SSTC_ Bhilai_CG
PPTX
10619416141061941614.106194161fff4..pptx
PPTX
String in java, string constructors and operations
PPTX
ppt8-string-1.pptx
PDF
05 c++-strings
DOCX
Unitii string
PPT
lecture5.ppt
PPT
lecture 5 string in c++ explaination and example.ppt
PPTX
Java string handling
PPT
Strings
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
PPTX
3 (3)Arrays and Strings for 11,12,college.pptx
DOCX
C Programming Strings.docx
PDF
String notes
PDF
C Strings oops with c++ ppt or pdf can be done.pdf
PPTX
Strings in Java
String in programming language in c or c++
lecture-5 string.pptx
String class and function for b.tech iii year students
C string _updated_Somesh_SSTC_ Bhilai_CG
10619416141061941614.106194161fff4..pptx
String in java, string constructors and operations
ppt8-string-1.pptx
05 c++-strings
Unitii string
lecture5.ppt
lecture 5 string in c++ explaination and example.ppt
Java string handling
Strings
Charcater and Strings.ppt Charcater and Strings.ppt
3 (3)Arrays and Strings for 11,12,college.pptx
C Programming Strings.docx
String notes
C Strings oops with c++ ppt or pdf can be done.pdf
Strings in Java

More from Michael Gordon (11)

PPTX
Raspberry Pi presentation for Computer Architecture class
PDF
Introduction to Linux, Pico, G++
PDF
Scope of variables
PDF
Functions
PDF
Shape logic 1
PDF
For loops
PDF
If statements
PDF
Arithmetic
PDF
Variables
PDF
PPTX
Word cloud
Raspberry Pi presentation for Computer Architecture class
Introduction to Linux, Pico, G++
Scope of variables
Functions
Shape logic 1
For loops
If statements
Arithmetic
Variables
Word cloud

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
medical staffing services at VALiNTRY
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Essential Infomation Tech presentation.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
top salesforce developer skills in 2025.pdf
PPTX
history of c programming in notes for students .pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Transform Your Business with a Software ERP System
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
medical staffing services at VALiNTRY
Softaken Excel to vCard Converter Software.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Essential Infomation Tech presentation.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
top salesforce developer skills in 2025.pdf
history of c programming in notes for students .pptx
PTS Company Brochure 2025 (1).pdf.......
Transform Your Business with a Software ERP System
Operating system designcfffgfgggggggvggggggggg
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
wealthsignaloriginal-com-DS-text-... (1).pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...

Strings1

  • 2. The String class  A class can be thought of as new type of variable that you can create.  C++ is also called “C with classes”  We’ve used one kind of class already: the string class, which is not in C.  string and its functions are built into C++.  C had C-strings, which can be found in the cstring library.
  • 3. Strings  The internal construction of a string (or cstring) is really an array of characters.  The first character of string s is s[0].  string s = “Hello”; cout<<s[4]; //prints the letter o  To find the length of a string s we can use the functions .length() or .size()  int x = s.length(); gives x the value of the size of the string s (in this case, 5).
  • 4. Strings  To “concatenate” two strings s1 and s2:  string s3 = s1 + s2;  string s1 = “race”;  string s2 = “car”;  string s3 = s1 + s2;  cout<<s3; //prints racecar
  • 5. String examples string first, last, name; cout<<"Enter your first name:"; cin>>first; cout<<"Enter your last name:"; cin>>last; cout<<"Hi, "+first+" "+last; //concatenation
  • 6. getline()  cin reads the typed string only until the first whitespace.  If you want the whole string including those whitespaces, you can use:  getline(cin, stringname);  cout<<“Enter your name:”;  getline(cin, fullname);  Use fullname instead of first and last.
  • 7. “dot” functions  With strings we start dealing with a new kind of function call. These functions operate on a specific “object” (an instance of a class).  We write the object name (e.g. the variable name of the string) followed by period and the function call (no space before or after the period).
  • 8. More functions  s1.find(string s2) – returns the index of the beginning of s2 in s1.  s1 = “hello”;  s1.find(“lo”) – returns 3  s1.insert(int i, string s2) – inserts s2 into s1 at index I  s1.at(int i) – returns the character at index i in string s1