C/C++ How Does C++ Handle Substring Searches?

Click For Summary
The discussion revolves around a C++ programming task that involves creating a substring function. The main requirement is to write a program that prompts the user to input a string and a substring. The program should then call a subfunction, mystrstr, which searches for the substring within the string and returns the starting position of the substring. If the substring is not found, the function should return -1. After determining the position, the main program will print the portion of the string starting from that position to the end. The original poster expresses concern about not finding relevant examples in their textbook and seeks assistance with the implementation of this function.
crimsondancer
Messages
1
Reaction score
0
I was reading my textbook to study for a test in my C++ class and I cam across this question and the answer is not in the book. I'm afraid I might need to know how to do something like this for my test and there isn't an example of one like it in my book. I'm not sure how to do it so I don't have any code to show you guys. I was wondering if anyone knew the answer to this question. Thanks!

Here it is:

Substring function

Write a program that finds a substring inside a string. Main program asks users to type a string and then a substring. Then, program calls a subfunction int mystrstr(char *str, char *substr). Inside the mystrstr subroutine, program searches substring substr in the string str and returns the starting position of the substring in the string. If the substring is not located in the string, then subroutine return -1. After searching the position, main program prints out the string from the position to the end of string of the string. e.g. string: "C++ program is really fun", substring: "program", Then, your program will print "program is really fun"
 
Technology news on Phys.org
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
73
Views
6K
  • · Replies 118 ·
4
Replies
118
Views
9K
Replies
89
Views
6K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 19 ·
Replies
19
Views
2K
Replies
69
Views
10K