Newbee and special characters

A

ATH0

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>

int letter=0;
int word=0;
int openBrace=0;
int closedBrace=0;
int completBrace=0;

char textLine[255]; // omited length

void main()
{
cout << "\n";
cout <<"Enter text line: ";
cin.getline(texLine,255);
_strupr(textLine);

for (int i=0; i<strlen(textLine); i++)
{
if (textLine != ' ')
word++;

if (textLine == '{') // special character
openBrace++;

if (textLine == '}') // special character
closedBrace++;

if (textLine == '{' || textLine == '}') //
special character
completBrace++;

if (textLine == ' ')
word++;
}

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";

}
 
K

Karl Heinz Buchegger

ATH0 said:
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?

Those headers are not standard C++
The standard headers are

#include <iostream>
#include said:
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 != ' ')
word++;

if (textLine == '{') // special character
openBrace++;

if (textLine == '}') // special character
closedBrace++;

if (textLine == '{' || textLine == '}') //
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 == ' ')
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 } }
 
V

Victor Bazarov

Karl said:
ATH0 said:


Those headers are not standard C++
The standard headers are

#include <iostream>
#include <string>
[...]

A nit-pick: <string.h> *is* actually a standard header. It's just
not the same as <string>. And, in fact, it's needed because the OP
uses 'strlen', not guaranteed to be declared in <string>.

V
 
A

ATH0

Thank you Karl and Victor for your replays...
I have to make this till tue. for my test exam so please
lets go one step at the time so i can catch up all of this :))


This are original stepes that i have to do (in order) :

a.) In code line there is NO leading empty places.
b.) Sign { is always last sign in line
c.) Sign } is always the only sign in line

---------------
I have to do:
---------------

0. ) User can enter unlimited number of lines in field

1.) Entered text has to have the parts described in a,b,c points

2.) Counting open braces ---> ( that are NOT closed ) and add at front of
the code line 4 empty places for each open brace. ---> this is the part that
i dont understand.

3.) If program finds closed brace in one line then he has to look if there
is open brace to combine this two. If this is the case ( completed braces )
then this open brace will NOT be counted. With other words, if program finds
{} then there is no open / close braces, it is "combined braces" and as such
you dont have to count them.

I have to make all this thru 2 functions ( entering input results, print
out)
***************************************************************

OK guys, am all ear. I hope we can use some of my code or if someone can
make copy paste
to each section that would be very nice of you. Like i say let go with code
example but easy so i can understand this....

ps. thank you for your support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top