title: Count occurrences of character in string c++?
body: <p>Enter 1st String: ronnald macdonnald </p>
  
  <p>Enter 2nd String: a </p>
  
  <p>Ouput: 3 </p>
  
  <p>Enter 1st String: ronnald macdonnald </p>
  
  <p>Enter 2nd String: nn </p>
  
  <p>Ouput: 2 </p>
  
  <pre><code>#include&lt;iostream&gt; 
  #include&lt;conio.h&gt; 
  using namespace std; 
  
  int main() 
  { 
  char a[100]; 
  char b; 
  int count = 0; 
  
  cout &lt;&lt; "Enter 1st String: "; 
  cin.getline(a,100); 
  cout &lt;&lt; "Enter 2nd String: "; 
  cin &gt;&gt; b; 
  
  for(int i=0;i&lt;strlen(a);i++) 
  { 
  if(a[i] == b) 
  { 
  count++; 
  } 
  
  } 
  cout &lt;&lt; "Output: " &lt;&lt; count; 
  
  getch(); 
  return 0; 
  } 
  </code></pre>
  
  <p>But in my code: </p>
  
  <p>Enter 1st String: ronnald macdonnald </p>
  
  <p>Enter 2nd String: nn </p>
  
  <p>Ouput: 4 </p>
  
  <p>The output should be 2</p>