Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning a substring using c++ within a class
    primarykey
    data
    text
    <p>SO I have a substring function that takes in the start location of the substring and its length. With that it should extract the characters within and return them as a string, without actually using any string function.</p> <pre><code>//default constructor that sets the initial string to the value "Hello World" MyString::MyString() { char temp[] = "Hello World"; int counter(0); while(temp[counter] != '\0') { counter++; } Size = counter; String = new char [Size]; for(int i=0; i &lt; Size; i++) String[i] = temp[i]; } //copy constructor MyString::MyString(const MyString &amp;source) { int counter(0); while(source.String[counter] != '\0') { counter++; } Size = counter; String = new char[Size]; for(int i = 0; i &lt; Size; i++) String[i] = source.String[i]; } </code></pre> <p>Here is my substring function:</p> <pre><code>MyString MyString::Substring(int start, int length) { char* leo = new char[length+1]; for(int i = start; i &lt; start + length+ 1; ++i) { leo[i-start] = String[i]; } MyString sub; delete [] sub.String; sub.String = leo; sub.Size = length+1; return sub; } </code></pre> <p>With this code from the main.cpp file:</p> <pre><code> int main (int argc, char **argv) { MyString String1; // String1 must be defined within the scope const MyString ConstString("Target string"); //Test of alternate constructor MyString SearchString; //Test of default constructor that should set "Hello World". MyString TargetString (String1); //Test of copy constructor cout &lt;&lt; "Please enter two strings. "; cout &lt;&lt; "Each string needs to be shorter than 256 characters or terminated by /\n." &lt;&lt; endl; cout &lt;&lt; "The first string will be searched to see whether it contains exactly the second string. " &lt;&lt; endl; cin &gt;&gt; SearchString &gt;&gt; TargetString; // Test of cascaded string-extraction operator if(SearchString.Find(TargetString) == -1) { cout &lt;&lt; TargetString &lt;&lt; " is not in " &lt;&lt; SearchString &lt;&lt; endl; } else { cout &lt;&lt; TargetString &lt;&lt; " is in " &lt;&lt; SearchString &lt;&lt; endl; cout &lt;&lt; "Details of the hit: " &lt;&lt; endl; cout &lt;&lt; "Starting position of the hit: " &lt;&lt; SearchString.Find(TargetString) &lt;&lt; endl; cout &lt;&lt; "The matching substring is: " &lt;&lt; SearchString.Substring(SearchString.Find(TargetString), TargetString.Length()-1)&lt;&lt;"\n"; } </code></pre> <p>It returns:</p> <p>Please enter two strings. Each string needs to be shorter than 256 characters or terminated by / . The first string will be searched to see whether it contains exactly the second string.</p> <p>forever</p> <p>more</p> <p>morev World is not in forever</p> <p>Any thoughts on why it actually isn't outputting the words from user input without the extra characters? I'm lost.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload