Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9301827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:07:06+00:00 2026-06-18T23:07:06+00:00

So I have this, and if I comment out the bottom part, from int

  • 0

So I have this, and if I comment out the bottom part, from int count = 0; to return 0; it will print, but in this case, nothing prints out. Even adding cout << "Test" in the beginning does nothing. It all compiles fine though.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    string text = "Smith, where Jones had had \"had had\", had had \"had\". \"Had had\" had had the examiners' approval.";

    string search = "had";

    int length = (int) text.length();

    for(int i = 0; i < length; i++)
    {
        text [i] = tolower(text [i]);
    }

    cout << text;

    int count = 0;
    for (int index = 0; (index = text.find(search)) != string::npos; index += search.length()) {
        count++;
        }

    cout << "There are " << count << " occurences of \"" << search << "\".\n"; 
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:07:07+00:00Added an answer on June 18, 2026 at 11:07 pm

    Compile it with g++ -g a.cpp then run it with gdb, you’ll find that it’s in an infinite loop.

    What is pointed out in @xymostech ‘s answer is correct though. Although if the loop does end, the buffer will be flushed before the code ends.

    Your pattern is always found in the string and therefore the text.find(search) never returns string::npos

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this string: comment_1234 I want to extract the 1234 from the string.
I have this link @Ajax.ActionLink(comment, CreateDebateComment, new { id = Model.DebateID}, new AjaxOptions {
I have this: $text = 'This is some text /*Comment 1 */ . Some
I have this in one of my haml views: -form_for :news_item_comment, :url=>{:id => comment.id,
I have this query, and I am trying to get the latest comment for
I have this json object : { msg_id:134, message:Dick, comment:[ { com_id:9, comment:test, msg_id:134,
I was doing a bit research and I came across this comment. I have
I've encountered many MATLAB codes which have a %#ok<SAGROW> comments. This comment is used
In this situation I have two models, Comment and Score. The relationship is defined
preg_match(/[\]/,'',$body); I use preg_match but I have this error here Compilation failed: missing terminating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.