ATH0 wrote:
How to search for special character { } and how to count them..
I got field called text ( undefined length ) and in this field you must
define "{" as start and "}" as end of some text line. If you can find both
{} then you have to count them.
example:
bla bla bla {bla { asd asd}
Result:
Words : 7
{ signs : 2
closed braces: 1
How can i make this?
--------------------------------------------------------------------
#include <iostream.h>
#include <string.h>
Those headers are not standard C++
The standard headers are
#include <iostream>
#include <string>
int letter=0;
int word=0;
int openBrace=0;
int closedBrace=0;
int completBrace=0;
char textLine[255]; // omited length
Don't use global variables, if you don't have a 100% good reason.
In your case there is no reason for them. Move those variables into
main()
Instead of error prone character arrays, you should prefer
std::string
void main()
main() returns int.
Always.
{
cout << "\n";
cout <<"Enter text line: ";
cin.getline(texLine,255);
_strupr(textLine);
What does that function do?
The leading _ indicates that it is not a standard C++ function.
for (int i=0; i<strlen(textLine); i++)
{
if (textLine[i] != ' ')
word++;
if (textLine[i] == '{') // special character
openBrace++;
if (textLine[i] == '}') // special character
closedBrace++;
if (textLine[i] == '{' || textLine[i] == '}') //
special character
completBrace++;
As you figured out, that will not work. What you do here, is in principle
identical to add up openBrace and closedBrace after the loop has terminated.
You just count how many '{' or '}' symbols are in the input.
if (textLine[i] == ' ')
word++;
This assumes that there is always a space character terminating a word.
For the moment this might be acceptable for you, but it is nevertheless
not intuitive. You might want to refine this.
}
word++; // for empty places
cout <<"# of words: " <<word<<endl;
cout <<"# of open braces: "<<openBrace<<endl;
cout <<"# closed braces: " <<closedBraces<<endl;
cout <<"# compleated braces: "<<completBrace<<endl;
cout << "\n";
}
How can i make this?
Rule #1: How would you do it, if you were a computer and need to answer
the very sam question?
I would do something like this.
(Assume the input looks eg. like this)
bla bla bla { bla { asd asd }
Identical to your program I would take a look at each character.
When I encounter a '{' I would increase a counter. When I encounter
a '}' I would look at that counter. If it is positive, then there has
been an '{' before, thus I have a match and I can decrease that counter
and record one pair:
counter: 0
complete braces: 0
bla bla bla { bla { asd asd }
^ ^ ^
| | |
counter: 1 ------+ | |
complete braces: 0 | |
| |
counter: 2 ------------+ |
complete braces: 0 |
|
counter: 1 ----------------------+
complete braces: 1
The end result tells me, that there were 1 complete braces, and
that there wasn't a closing brace for exactly 1 opening brace.
counter: 0
complete braces: 0
bla { bla { bla { bla } { asd asd } }
^ ^ ^ ^ ^ ^ ^
| | | | | | |
counter: 1 --+ | | | | | |
complete braces: 0 | | | | | |
| | | | | |
counter: 2 --------+ | | | | |
complete braces: 0 | | | | |
| | | | |
counter: 3 --------------+ | | | |
complete braces: 0 | | | |
| | | |
counter: 2 --------------------+ | | |
complete braces: 1 | | |
| | |
counter: 3 ----------------------+ | |
complete braces: 1 | |
| |
counter: 2 --------------------------------+ |
coplete braces: 2 |
|
counter: 1 ----------------------------------+
complete braces: 3
So the final result tells us, that there were 3 complete brace pairs
and 1 opening brace which didn't get a closing brace.
Now that you have that strategy on paper, it should be fairly easy to implement
it.
Exercise left to the reader:
What would you do, to figure out if there are closing braces, which don't have an opening
brace? As in:
bla bla } bla { bla } }
--
Karl Heinz Buchegger
kb******@gascad.at