Having major problems(C++ student) trying to figure the code that will report the number of instances of a character in a string.

Using elementary code (Borland Builder5)
The section of code has to:

read in a string value from the keyboard
read in a character from the keyboard
report the number of occurrences of the character
then replace the selected character with the asterisk character and write out the revised string.

Any help appreciated

06-13-2002

jdinger

OK and what code have you tried so far? Are you using strcmp() to test for character matches? What errors are you getting when you try to parse the string?

06-13-2002

steveg

characters in a string

As you will see I am in a state of confusion, I just cannot get my head round the concept of the code required. Am I being clear enough in what i am trying to do?

Where are the implementations for ReadStringPr(), ReadCharPr() and WriteIntPrCr()?

06-13-2002

Prelude

Just start at the beginning of the string and go through each character, testing for the key each time. If the two characters match then replace the one in the string with an asterisk and increment a counter:

This is a small sidequestion to Prelude..I notice that you use std:: specific instead of using namespace std. I am curious for your reasoning behind that style? (I might mention that I haven't yet decided for a style myself)

:confused: I seem out of my depth here, dont want to waste peoples time but do want understand the subject. The ReadStringPr etc. are as I have used them in my course so far. (Would the fact i'm on a course modified version of Borland builder have any bearing on the matter?)

06-13-2002

Prelude

>I am curious for your reasoning behind that style?
When you open the entire namespace globally, it fills the global scope with unnecessary names. This is exactly the opposite of what namespaces were designed for, so I opted for the safety over convenience method instead of the more commonly used convenience over safety.

In general, you want as little as possible open to the global scope. This goes for namespaces as well as variables, so it's considered good practice to specify namespace instead of open everything with using statements.

>Would the fact i'm on a course modified version of Borland
>builder have any bearing on the matter?
Not if your school knows what they're doing...which they probably don't. It's best to stick with the standard I/O classes and functions instead of relying on the hacked up code that most schools seem to be so skilled at.

-Prelude

06-13-2002

jdinger

Quote:

Originally posted by steveg The ReadStringPr etc. are as I have used them in my course so far.

So how did the functions work in your course? Prelude just handed you your homework on a platter. What do you not understand? You read through a string (char array) testing for instances of a predefined char and incrimenting an int to reflect the total number of instances.