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 7054529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:38:17+00:00 2026-05-28T03:38:17+00:00

This is actually my homework and the question states: The program should determine how

  • 0

This is actually my homework and the question states:

“The program should determine how many times the character is
contained in the string. (Hint: Search the string by using the
find(str,ƒind) method. This method should be used in a loop that
starts the index value at 0 and then changes the index value to 1 past
the index of where the char was last found.)”

This is what I’ve came up with but all it does is count how many character there is in the string. New to C++ so I hope you guys can be patient with me.

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

int main()
{
    string s;
    char c;
    size_t contain;
    int count = 0;
    cout << "Enter a string : ";
    getline(cin, s);
    cout <<"Enter a char : ";
    cin >> c;
    for(int i = 0; i < s.length(); i++)
    {
        contain = s.find(c, i);
        if (contain =! string::npos )
        {
            count++;
        }
    }
    cout << count <<endl;
    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-05-28T03:38:18+00:00Added an answer on May 28, 2026 at 3:38 am

    I think @parapura’s code looks nice’er like this:

    while((size_t contain = s.find(c,i)) != string::npos){
        count++;
        i = contain + 1;
    }
    

    and it solves the problem nicely 😉

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

Sidebar

Related Questions

This is NOT a homework question, actually I am doing this for fun. Here
This isn't actually a homework question per se, just a question that keeps nagging
I'm newbie in Python, but this question is not a homework (actually this code
Simple question - what does this actually do? var oq = (ObjectQuery<TEntity>)L2EQuery; return ExecuteFirstorDefault<TEntity>(oq,
This is actually a two part question. First,does the HttpContext.Current correspond to the current
This is actually a non-critical question, but I get this warning most of the
This is actually related to another question I had that was already answered. That
Problem This question actually came up at work today. We are planning an experiment
This is a question for a homework problem that I cant figure out: Question
Well, to be honest this is actually my homework where I have to implement

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.