title: Searching for the frequency of a word in a string (C++)
body: <p>I have to write a program that asks the user for a sentence (with a limit of 1024 letters), then asks the user for a word, and tells the user how many times that specific word occurs in the sentence, for an assignment. </p>
  
  <p>We're only allowed to use these libraries: iostream, string, cstring, cstdlib, cmath and fstream.</p>
  
  <p>example of how it's supposed to function:</p>
  
  <p>Enter a sentence:</p>
  
  <p>input:hello my name is hello hello</p>
  
  <p>Enter a word to check for frequency:</p>
  
  <p>input:hello</p>
  
  <p>desired output:# of times word occurs: 3</p>
  
  <p>actual output:# of times word occurs: 25</p>
  
  <p>This is what I tried using with the help of a friend, but when I enter a word to search it outputs an unrelated number.</p>
  
  <pre><code>int wordFrequency(){
      char sen5[1024];
      int frequency = 0;
      char word[1024];
      cout &lt;&lt; "Enter a sentence: " &lt;&lt; endl;
      cin.getline(sen5, 1024);
      cout &lt;&lt; "Enter a word to check for frequency: " &lt;&lt; endl;
      cin.getline(word, 1024);
      for(int i = 0; i &lt; strlen(sen5); i++){
              if(sen5[i] == word[i]){
                      for(int j = 0; j &lt; strlen(word); j++)
                              if(sen5[j] == word[j])
                                      frequency += 1;
              }
      }
      cout &lt;&lt; "# of times word occurs: " &lt;&lt; frequency &lt;&lt; endl;
      return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>Also, I'm aware my coding is horrid, my professor is notoriously horrible and I'm struggling immensely in my class because of it. Any help is appreciated.</p>