Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I guess you need this for studying functions and parameters. I have made minimal changes to your code, just to show how to put the code in a function.</p> <pre><code>#include &lt;iostream&gt; #include &lt;cstdio&gt; #include &lt;string&gt; #include &lt;iomanip&gt; using namespace std; // the function is here bool searchWord(char* sentence, char* search) { int i, len; int j, lenS; len = strlen(sentence); lenS = strlen(search); bool found = false; for (i = 0; i &lt; len - lenS + 1; i++) { if (sentence[i] == search[0]) { for (j = 0; j &lt; lenS; j++) { if (sentence[i+j] != search[j]) break; } if (j == lenS) { //cout &lt;&lt; "search found\n"; found = true; } } } if (i == len - lenS +1) { found = false; } return found; } int main () { char mySentence[50]; char mySearch [50]; int numtimes = 0; cout &lt;&lt; "Enter a sentence: " &lt;&lt; endl; gets (mySentence); cout &lt;&lt; endl; cout &lt;&lt; "Your sentence is: \n" &lt;&lt; mySentence &lt;&lt; endl; cout &lt;&lt; "Enter a any word: " &lt;&lt; endl; gets (mySearch); cout &lt;&lt; endl; cout &lt;&lt; "The word is: " &lt;&lt; mySearch &lt;&lt; endl; if (searchWord(mySentence, mySearch)) { cout &lt;&lt; "Found\n"; } else { cout &lt;&lt; "Not Found\n"; } system("PAUSE"); return 0; } </code></pre> <p>Two things to note:</p> <ol> <li><p>You might want to check for a space after finding the last letter of the word. Example: Sentence: The sentences should be English Word: sentence</p></li> <li><p>string.h has several functions that can be handy in this type of tasks.</p></li> </ol>
    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.
    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