PDA

View Full Version : Writing a program in C


iasc
Oct24-09, 08:09 AM
I'm trying to write a piece of code for the rule 'i before e except after c'.

Heres what I have so far.
if(str[4]=='c')
&&
if(str[5]=='ei')
printf ("yes\n");
else
printf("no\n");

I don't really want the answer but I just want to know if I'm going in the right direction.

sylas
Oct24-09, 08:20 AM
I'm trying to write a piece of code for the rule 'i before e except after c'.

Heres what I have so far.
if(str[4]=='c')
&&
if(str[5]=='ei')
printf ("yes\n");
else
printf("no\n");

I don't really want the answer but I just want to know if I'm going in the right direction.

No.

Your C syntax is pretty badly messed up. You can't have two characters in a single position of an array, so str[5] == 'ei' doesn't work. You would normally need to scan through the str array, rather than looking at one position. The && operator can't be used to combine statements.

Cheers -- sylas

technoweasel
Oct24-09, 12:13 PM
It seems like you don't know a lot about C yet (no offense). If you are learning by yourself, I would recommend two books. First, "C Pocket Handbook" (or something like that), which is a small paperback from Wiley that has a cow on the front and is only around $10. Second, you might want to get "C++: the Complete Reference" by Herbert Schildt. It really is a complete guide to C, and one of the best books I've seen.

For now, don't worry about the ++ in C++. To me, it seems that C++ does little (except OOP) that C doesn't, so use the easier C-style io, etc.

Furthermore, remember to start small. Don't try to begin by writing a game or something. Do a few simple console programs like Conway's Game of Life.

Good luck.

HallsofIvy
Oct24-09, 02:22 PM
I'm trying to write a piece of code for the rule 'i before e except after c'.

Heres what I have so far.
if(str[4]=='c')
&&
if(str[5]=='ei')
printf ("yes\n");
else
printf("no\n");

I don't really want the answer but I just want to know if I'm going in the right direction.
I suspect you want
"if (str[4]== 'c') & (str[5]== 'e')

Notice the ( ) arround the entire compound statement, "&" rather than "&&" and a single character for str[5] rather than two.

sylas
Oct24-09, 04:08 PM
I feel bad about my first reply. It was a bit abrupt. Sorry! Practicing programming is essential for learning, and it always involves making lots of mistakes. Don't worry about that... it's normal.

But I echo the advice to start small. You are better to start out with simple programs not using arrays. Something with numbers, for example. Write a program to read numbers and calculate something. Then move to if statements, then loops, and only then arrays.

I suspect you want
"if (str[4]== 'c') & (str[5]== 'e')

Notice the ( ) arround the entire compound statement, "&" rather than "&&" and a single character for str[5] rather than two.

I think "&&" is more appropriate. The "&" operator is a bitwise and. It will, in this case, happen to give the same result; but in general this is a logical comparison and the logical and operator "&&" is more appropriate. You also need another layer of brackets with the if statement.

Here's a step towards the solution to the problem posed. Let's think about what is required. The rule is "i before e except after c".

Generally, this means you want to see if the condition is satisfied by the word. The condition fails when you have a "e before i, with no c before hand".

You also may want to check for the pattern "cie".

The test would be:

if ( str[3] == 'c' && str[4] == 'i' && str[5] == 'e' ) {
printf("There is an 'ie' in the word, coming after a 'c'.\n");
} else if ( str[3] != 'c' && str[4] == 'e' && str[5] == 'i' ) {
printf("There is an 'ei' in the word, and it is not after a 'c'.\n");
} else {
printf("The word is okay for the rule at the one place I looked.\n");
}

Note that this looks for two different ways the rule can fail.

Here is a loop going through a null terminated array, looking for violations of "i before e except after c". This time I am using multiple if statements.


for ( i = 0; str[i+2] != 0; i++ ) {
if ( str[i] == 'c' ) {
if ( str[i+1] == 'i' && str[i+2] == 'e' ) {
printf("Word contains i before e after a c\n");
}
} else {
if ( str[i+1] == 'e' && str[i+2] == 'i' ) {
printf("Word contains e before i, and it isn't after a c\n");
}
}
}

Mark44
Oct24-09, 08:39 PM
I suspect you want
"if (str[4]== 'c') & (str[5]== 'e')

Notice the ( ) arround the entire compound statement, "&" rather than "&&" and a single character for str[5] rather than two.

The logical and operator, &&, is correct. The & operator is the bitwise and operator, and is used for something quite different. For example, the following code produces z is 4 as output. The explanation for this is that each bit in x is compared with the corresponding bit in y. If both bits are set (equal to 1), the result is a bit set to 1. If the two bits are different, the result is a bit set to 0.


int x = 5, y = 4;
int z;

z = x & y;
printf("z is %d", z);

silverfrost
Oct25-09, 04:20 AM
Here is a loop going through a null terminated array, looking for violations of "i before e except after c". This time I am using multiple if statements.


for ( i = 0; str[i+2] != 0; i++ ) {
if ( str[i] == 'c' ) {
if ( str[i+1] == 'i' && str[i+2] == 'e' ) {
printf("Word contains i before e after a c\n");
}
} else {
if ( str[i+1] == 'e' && str[i+2] == 'i' ) {
printf("Word contains e before i, and it isn't after a c\n");
}
}
}



Make sure this code is guarded correctly -- it will not work for single letter words (as str[i+2] is already past the terminator).

sylas
Oct25-09, 06:05 AM
Make sure this code is guarded correctly -- it will not work for single letter words (as str[i+2] is already past the terminator).

Quite true. There's a lot omitted here, of course. In detail you'd write a function, and return true or false from the function.

Borek
Oct25-09, 06:47 AM
I suspect you want
"if (str[4]== 'c') & (str[5]== 'e')

Notice the ( ) arround the entire compound statement, "&" rather than "&&" and a single character for str[5] rather than two.

Funny thing is - in this particular case it won't matter if it is & or &&, as TRUE is just 1. But in general Mark44 got it right.