I need help in writing a the following programs in C language: We want to determine whether a long string of a’s and b’s has the property that the number of a’s is even and the number of b’s is even.

Algorithm 1: Keep two binary flags, the a-flag and the b-flag. Every time an a is read, the a-flag is reversed (0 to 1, or 1 to 0); and every time a b is read, the b-flag is reversed. We start both flags at 0 and check to be sure they are both 0 at the end.

Algorithm 2: Keep only one binary flag, called the type3-flag. We read letter in two at a time. If they are the same, then we do not touch the type3-flag, since we have a factor of type1 or type2. If, however, the two letters do not match, we reverse the type3-flag. If the flag starts at 0 and if it is also 0 at the end, then the input string contains an even number of a’s and an even number of b’s.

Why do you expect us to do YOUR homework?

commented: I believe so :D +6

Write the code. Post it here if you have problems or errors. Then we may help you.

i think u can use friend function..

Do you know how to input a long C string?

In a loop, you could prompt (use printf) and then use fgets and a large char buffer (length pre-fixed at compile time) to input your test C string of a's and b's ... or you could use a readLine function to read in a dynamic C string of any length.

http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864

Then just code using the hints you were given:

/* initial flags */
int a = 0, b = 0, c = 0;

Algorithm 1: Keep two binary flags, the a-flag and the b-flag. Every time an a is read, the a-flag is reversed (0 to 1, or 1 to 0); and every time a b is read, the b-flag is reversed. We start both flags at 0 and check to be sure they are both 0 at the end.

Algorithm 2: Keep only one binary flag, called the type3-flag. We read letter in two at a time. If they are the same, then we do not touch the type3-flag, since we have a factor of type1 or type2. If, however, the two letters do not match, we reverse the type3-flag. If the flag starts at 0 and if it is also 0 at the end, then the input string contains an even number of a's and an even number of b's.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.