help finding a char in a string

Finding a char in a string of char's

Page 1 of 1

6 Replies - 1446 Views - Last Post: 22 November 2009 - 05:32 PM Rate Topic: -----

#1 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

help finding a char in a string

Posted 22 November 2009 - 12:43 AM

Hi everyone! I was wondering has anyone came across the find(str, ind)?

I need to find a "char" in a string, so the code dose not do that purpose. The count++ dose not increment the way it should so the number of time say the leter "e" would appear in the string is 47 it should be 7 times. Dose anyone see what is missing in my code. thanks for any input.



#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <limits>
#include <string>
using namespace std;



int main()
{
char ch;
int count = 0;
	
char buffer[] = "She comes from a broken home, a tree fell on it";
cout << "Enter a letter to find in the string : ";
cin >> ch;
cout << "\n"<<buffer <<endl;

for (int i = 0; i < (strlen(buffer)); i++)
{
	if (buffer[i] == ch)
	{
	count++;
	}
}

cout << "Your leter "<<ch<<" is in the string " <<count<<" times "<< " with string size of "<< strlen(buffer)<<endl;

/*(Lastname_Lab9_p1.cpp)
Write a program that accepts both a string and a single character from the user. The
program should then determine how many times the character is contained in the string.
(Hint: Search the string using the find(str, ind) method). The method should be
used in a loop that starts the index value at zero, and then changes the index to one value
past the index of where the char was last found.*/		 
	

system("pause");
}


This post has been edited by Omegaclass: 22 November 2009 - 01:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: help finding a char in a string

#2 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: help finding a char in a string

Posted 22 November 2009 - 12:45 AM

You output count before you set count a value besides 0.
Was This Post Helpful? 1
  • +
  • -

#3 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

Re: help finding a char in a string

Posted 22 November 2009 - 12:52 AM

View Postccubed, on 21 Nov, 2009 - 11:45 PM, said:

You output count before you set count a value besides 0.



i know it was stupid of me but i fixed it and edited the post, but now it says the letter "e" shows up 47 times
, what could it be?
Was This Post Helpful? 0
  • +
  • -

#4 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: help finding a char in a string

Posted 22 November 2009 - 01:14 AM

have you tried string compare?

http://www.cplusplus...cstring/strcmp/
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: help finding a char in a string

Posted 22 November 2009 - 01:24 AM

Read your message again, it's working fine.

Enter a letter to find in the string : e

She comes from a broken home, a tree fell on it
Your leter e is in the string 7 times  with string size of 47


Was This Post Helpful? 1

#6 Omegaclass  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-November 09

Re: help finding a char in a string

Posted 22 November 2009 - 04:08 PM

View PostJackOfAllTrades, on 22 Nov, 2009 - 12:24 AM, said:

Read your message again, it's working fine.

Enter a letter to find in the string : e

She comes from a broken home, a tree fell on it
Your leter e is in the string 7 times  with string size of 47




your right, it dose work! i don't know why i kept getting 47 for count, thanks.

the question ask that i use find(str, ind) as a possible way to do it, but my search on this hasn't turned up anything, any suggestions?
Was This Post Helpful? 0
  • +
  • -

#7 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: help finding a char in a string

Posted 22 November 2009 - 05:32 PM

View PostOmegaclass, on 22 Nov, 2009 - 03:08 PM, said:

View PostJackOfAllTrades, on 22 Nov, 2009 - 12:24 AM, said:

Read your message again, it's working fine.

Enter a letter to find in the string : e

She comes from a broken home, a tree fell on it
Your leter e is in the string 7 times  with string size of 47




your right, it dose work! i don't know why i kept getting 47 for count, thanks.

the question ask that i use find(str, ind) as a possible way to do it, but my search on this hasn't turned up anything, any suggestions?


I can tell you how it works, but not how to use it. I tried to use it awhile back and kept getting undefined errors. After 15 minutes of this and including almost every string/c-string include there is available, I ended up writing my own function out of frustration.

Basically, this.

string a ="This is a test";
a.find("is"); // results in 5, which is where the first instance starts



ind is for index, which is how you tell it where to start in the string. Really, it's a pointless wrapper for looping through the string as a char array and finding the first instance that the word to find occurs at.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1