📖 Scenario: Imagine you are building a simple text search tool that helps users find specific words inside a paragraph. This tool will look for a smaller piece of text (called a substring) inside a bigger text.
🎯 Goal: You will create a program that searches for all the positions where a given substring appears inside a larger string. This is useful for finding patterns or keywords in documents.
📋 What You'll Learn
Create a string variable for the main text
Create a string variable for the substring to search
Use a loop to check each possible position in the main text
Print all starting positions where the substring is found
💡 Why This Matters
🌍 Real World
Searching for keywords or patterns inside documents, logs, or messages is common in text editors, search engines, and data analysis tools.
💼 Career
Understanding substring search is fundamental for roles in software development, data processing, and quality assurance where text manipulation is frequent.
Progress0 / 4 steps
1
Create the main text string
Create a string variable called text and set it to the exact value "hello world, welcome to the world of programming".
DSA C++
Hint
Use std::string to create the variable text and assign the given sentence.
2
Create the substring to search
Add a string variable called pattern and set it to the exact value "world".
DSA C++
Hint
Create the pattern string just like you did for text.
3
Find all positions of the substring
Use a for loop with an integer variable i from 0 to text.length() - pattern.length(). Inside the loop, use text.substr(i, pattern.length()) == pattern to check if the substring matches. If it matches, print i followed by a space.
DSA C++
Hint
Loop through text and compare each substring of length equal to pattern with pattern.
4
Print the final output
Add a std::cout statement after the loop to print a newline character \n for clean output.
DSA C++
Hint
Use std::cout << "\n"; to move to the next line after printing all positions.
Practice
(1/5)
1. What is the main goal of substring search algorithms in strings?
easy
A. To count the number of vowels in a string
B. To sort the characters of a string alphabetically
C. To reverse the entire string
D. To find if a smaller string exists inside a larger string
Solution
Step 1: Understand substring search purpose
Substring search algorithms check if a smaller string (pattern) appears inside a bigger string (text).
Step 2: Compare options with definition
Only To find if a smaller string exists inside a larger string describes finding a smaller string inside a larger one, which matches substring search.
Final Answer:
To find if a smaller string exists inside a larger string -> Option D
Hint: Substring search means finding a smaller string inside a bigger one [OK]
Common Mistakes:
Confusing substring search with sorting or reversing strings
Thinking substring search counts characters instead of finding patterns
2. Which of the following is the correct syntax to check if string pattern is a substring of string text in C++?
easy
A. if (text.find(pattern) != std::string::npos) { /* found */ }
B. if (pattern.contains(text)) { /* found */ }
C. if (text.indexOf(pattern)) { /* found */ }
D. if (text.search(pattern) == true) { /* found */ }
Solution
Step 1: Recall C++ substring search syntax
In C++, std::string::find returns the position of the substring or std::string::npos if not found.
Step 2: Analyze each option
if (text.find(pattern) != std::string::npos) { /* found */ } uses text.find(pattern) != std::string::npos, which is correct. Others use invalid or non-existent methods.
Final Answer:
if (text.find(pattern) != std::string::npos) { /* found */ } -> Option A
Quick Check:
Use find() and compare with npos [OK]
Hint: Use text.find(pattern) != npos to check substring [OK]
Common Mistakes:
Using methods like contains or indexOf which don't exist in std::string
Comparing find() result directly to true or false
3. What will be the output of the following C++ code?