0% found this document useful (0 votes)
24 views9 pages

Just Stringing You Along-1

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)
24 views9 pages

Just Stringing You Along-1

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/ 9

Just stringing

you along…
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”;


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

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 fullName = “strings can be fun!”;


index = fullName.find(“s”);
findThe2ndS = fullName.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(“.”);
firstName = loginName.substr(0, index);
index = loginName.find(“.”, ++index);
extension = loginName.substr(index + 1, 3);
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
• Validate user input
• Social Security Number: xxx-xx-xxxx
• Email: xxxxx@xxxxx.xxx
• Passwords: Must contain an upper-case letter. Must contain a number. Must be a certain
length.

• Analyze a text document: After analysis, your file contains 45 spaces, 576 words,
24 sentences, 72 digits…

• Create username:
Enter first name: David
Enter last name: Ray
Enter SSN: 444-33-1234

Your user name is dray1234


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