CPPForSchool.com: String Set 1 Assignment 7
7.
Write a program to find a substring within a string. If found display its starting position.
This assignment had me quite clueless as how to create a substring within a string. With that said, I searched online for examples and began coding accordingly. However, after reviewing the sample source code, I quickly discovered that I jumped ahead of the curve. Below is an example of using the string library. It is important to note that I have updated the program and provided additional screenshots for comparison.
For my first attempt, I utilized the example from GeeksForGeeks. Ultimately, the user will need to input two words separated by a colon. From there, the program will locate the colon and separate the second word into its own variable, in this case "sub".
In regards to using external libraries, I have become quite fond of the concept. Anything to get the ball rolling, I guess. Now, the source code below is the revised version of this assignment. This rendition follows the example source code in order to accomplish the substring acquisition.
This assignment had me quite clueless as how to create a substring within a string. With that said, I searched online for examples and began coding accordingly. However, after reviewing the sample source code, I quickly discovered that I jumped ahead of the curve. Below is an example of using the string library. It is important to note that I have updated the program and provided additional screenshots for comparison.
For my first attempt, I utilized the example from GeeksForGeeks. Ultimately, the user will need to input two words separated by a colon. From there, the program will locate the colon and separate the second word into its own variable, in this case "sub".
In regards to using external libraries, I have become quite fond of the concept. Anything to get the ball rolling, I guess. Now, the source code below is the revised version of this assignment. This rendition follows the example source code in order to accomplish the substring acquisition.
Although everything seemed to be working correctly, I was able to discover something wrong with this program. It appears that the current algorithm is unable to take into interpret words with double letters. As seen below, the first string I entered was "stopping". For fun, I tested the word "ping", which unfortunately did not work as expected. Perhaps this is why there is a string library to use in regards to substrings. Nevertheless, I have also included screenshots of the sample source code and output for comparison.
Comments
Post a Comment