Writing a C Program for "I Before E Except After C" Rule

  • Thread starter Thread starter iasc
  • Start date Start date
  • Tags Tags
    Program Writing
Click For Summary
The discussion revolves around coding the rule "i before e except after c" in C programming. The original code attempts to check specific characters in a string but contains syntax errors and misunderstandings about how to access string elements. Key points include the incorrect use of array indexing, as the code tries to check two characters at once, which is not valid in C. Participants emphasize the importance of using the logical AND operator "&&" instead of the bitwise operator "&" for proper logical comparisons. They suggest starting with simpler programming tasks before tackling more complex rules. A more effective approach is proposed, involving loops to scan through the string and check for the specific patterns that violate the rule. The conversation highlights the necessity of proper syntax and understanding of C programming fundamentals for beginners.
iasc
Messages
17
Reaction score
0
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.
 
Technology news on Phys.org
iasc said:
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
 
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.
 
iasc said:
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.
 
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.

HallsofIvy said:
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:
Code:
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.

Code:
    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");
            }
        }
    }
 
Last edited:
HallsofIvy said:
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.

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

z = x & y;
printf("z is %d", z);
 
sylas said:
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.

Code:
    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).
 
silverfrost said:
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.
 
HallsofIvy said:
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
14
Views
3K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
55
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
47
Views
5K
  • · Replies 97 ·
4
Replies
97
Views
9K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K