Challenge: Searching Within Strings
Method find()
Sometimes, you may need to search for specific text within a string
. This can be done using the .find()
or .rfind()
methods. Let’s begin with the .find()
method.
find.h
1str.find("text to find")
It returns the index of the first character of the first match.
main.cpp
1234567#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; // (code)codefinity }
You can also specify the position of the first character in the string to be considered in the search. It can be done using the pos
argument. Any characters before the pos
index are ignored in a search.
Here is an example of finding the first "code"
starting from a 3-rd character.
main.cpp
1234567#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code", 3) << std::endl; // __de(code)finity }
Method rfind()
You can also locate the last occurrence of some text using the .rfind()
method.
rfind.h
1str.rfind("text to find")
While .find()
retrieves the first occurrence of text, .rfind()
finds the last occurrence and stands for reverse find.
main.cpp
12345678#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; std::cout << str.rfind("code") << std::endl; }
Note
If no match is found, both
.find()
and.rfind()
return a special value,string::npos
. This value represents no position and indicates that the search was unsuccessful.
main.cpp
12345678#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("abc") << std::endl; std::cout << std::string::npos << std::endl; }
Swipe to start coding
You are building a simple email filter. Your goal is to allow users to register with any email and mark emails from codefinity.com
as allowed.
The function isAllowedEmail
takes an email
as string
.
- Use
rfind()
to find the last'@'
symbol in the email. - Compare the result of
rfind
with-1
to check if the'@'
symbol exists. If no'@'
is found, returnfalse
because the email is invalid. - Initialize a string variable
domain
as an empty string. - Use a
for
loop starting fromatPos + 1
up to the length ofemail
to iterate over characters after the'@'
. - In each iteration, append the character to the
domain
variable. - If
domain
equals"codefinity.com"
, returntrue
. - Otherwise, return
false
.
Example
isAllowedEmail("alice@example.com")
→ false
isAllowedEmail("bob@codefinity.com")
→ true
isAllowedEmail("john.doe@gmail.com")
→ false
isAllowedEmail("noatdomain.com")
→ false
Løsning
solution.cpp
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you show an example of using the .find() method?
What does string::npos mean in practice?
How is .rfind() different from .find()?
Awesome!
Completion rate improved to 4.35
Challenge: Searching Within Strings
Stryg for at vise menuen
Method find()
Sometimes, you may need to search for specific text within a string
. This can be done using the .find()
or .rfind()
methods. Let’s begin with the .find()
method.
find.h
1str.find("text to find")
It returns the index of the first character of the first match.
main.cpp
1234567#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; // (code)codefinity }
You can also specify the position of the first character in the string to be considered in the search. It can be done using the pos
argument. Any characters before the pos
index are ignored in a search.
Here is an example of finding the first "code"
starting from a 3-rd character.
main.cpp
1234567#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code", 3) << std::endl; // __de(code)finity }
Method rfind()
You can also locate the last occurrence of some text using the .rfind()
method.
rfind.h
1str.rfind("text to find")
While .find()
retrieves the first occurrence of text, .rfind()
finds the last occurrence and stands for reverse find.
main.cpp
12345678#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.find("code") << std::endl; std::cout << str.rfind("code") << std::endl; }
Note
If no match is found, both
.find()
and.rfind()
return a special value,string::npos
. This value represents no position and indicates that the search was unsuccessful.
main.cpp
12345678#include <iostream> int main() { std::string str = "codecodefinity"; std::cout << str.rfind("abc") << std::endl; std::cout << std::string::npos << std::endl; }
Swipe to start coding
You are building a simple email filter. Your goal is to allow users to register with any email and mark emails from codefinity.com
as allowed.
The function isAllowedEmail
takes an email
as string
.
- Use
rfind()
to find the last'@'
symbol in the email. - Compare the result of
rfind
with-1
to check if the'@'
symbol exists. If no'@'
is found, returnfalse
because the email is invalid. - Initialize a string variable
domain
as an empty string. - Use a
for
loop starting fromatPos + 1
up to the length ofemail
to iterate over characters after the'@'
. - In each iteration, append the character to the
domain
variable. - If
domain
equals"codefinity.com"
, returntrue
. - Otherwise, return
false
.
Example
isAllowedEmail("alice@example.com")
→ false
isAllowedEmail("bob@codefinity.com")
→ true
isAllowedEmail("john.doe@gmail.com")
→ false
isAllowedEmail("noatdomain.com")
→ false
Løsning
solution.cpp
Tak for dine kommentarer!
single