0% found this document useful (0 votes)
23 views11 pages

Just Stringing You Along-2

Uploaded by

hayden.hunt38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views11 pages

Just Stringing You Along-2

Uploaded by

hayden.hunt38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Just stringing

you along…
Without knowing it, you’ve been
using objects in your previous C++
class

String #include <string> 🡪 pulls in the


string class definition so that you
datatype can work with strings.

String class includes many functions


(methods) that allow you to
manipulate string data
How to call String functions
#include <string>
string myString;

General syntax:
stringVarName.stringFunctionName()

Example:
myString.length()
String functions (length)
myString.length() 🡪 Returns the length of the string in myString

string firstName = “David”;


cout << “First name has “ << firstName.length() << “ letters.”;

First name has 5 letters.


String functions (find)
myString.find(str) 🡪 Returns the first position where the string str is
found in myString

string fullName = “Elon Musk”;


int position = fullName.find(“Musk”);
cout << “Musk starts at position “ << position);

Musk starts at position 5


String functions (find at example)
myString.find(str, p) 🡪 Returns the first position at or beyond position
p where the string str is found in myString

string sentence = “strings can be fun!”;


index = sentence.find(“s”);
findThe2ndS = sentence.find(“s”, index + 1);
cout << “The 2nd S was found at position “ << findThe2ndS;

The 2nd S was found at position 6


String functions (SUBSTRing)
myString.substr(p, n) 🡪 Returns a copy of a substring. The substring is n
characters long and begins at position p of myString

string loginName = “david.ray@jcjc.edu”;


index = loginName.find(“.”); 🡪 index = 5
firstName = loginName.substr(0, index); 🡪 firstName = david
index = loginName.find(“.”, ++index); 🡪 index = 14
extension = loginName.substr(index + 1, 3); 🡪 extension = edu

cout << firstName << “ has an email address with an extension of “ << extension;

david has an email address with an extension of edu


Things we could do with string functions…

Validate Analyze Create

Validate user input Analyze a text Create username:


• Social Security Number: document: • Enter first name: David
xxx-xx-xxxx • Enter last name: Ray
• Email: xxxxx@xxxxx.xxx After analysis, your • Enter SSN: 444-33-1234
• Passwords: Must • Your user name is
contain an upper-case
file contains 45
dray1234
letter. Must contain a spaces, 576 words,
number. Must be a 24 sentences, 72
certain length. digits…
char ch;
int vowelCount = 0;
string sentence;
cout << "Enter any sentence you wish, and I will tell you how many vowels are in it.\n";
getline(cin, sentence);
for (int pos = 0; pos < sentence.length(); pos++)
{
ch = toupper(sentence[pos]); e ls a re in it.
m a ny vow
switch(ch) ll te ll you ho w
, a n d Iw i
g. [ Enter]
{ te n ce you wis
h
v e r t he lazy d
o
r a ny sen u m pe d o
case 'A’: En te
k b ro w nf ox j
e s e nt e nce.
T he quic 2 vowels in th
case 'E’: re 1
There a
case 'I’:
case 'O’:
case 'U’: vowelCount++;
}
}
cout << "There are " << vowelCount << " vowels in the sentence.\n";
String Methods
String Methods

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy