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

In summary, the conversation is about a person trying to write a piece of code for the rule 'i before e except after c'. They share their current code and ask for feedback on whether they are going in the right direction. Another person provides some helpful tips and suggests starting small with simple programs. They also provide a solution to the problem by using multiple if statements and a loop to check for violations of the rule.
  • #1
iasc
17
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
  • #2
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
 
  • #3
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.
 
  • #4
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.
 
  • #5
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:
  • #6
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);
 
  • #7
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).
 
  • #8
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.
 
  • #9
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.
 

What is the "I before E except after C" rule?

The "I before E except after C" rule is a common spelling rule in English that states that when the letters "I" and "E" appear together in a word, "I" comes before "E" unless it follows the letter "C".

How do I write a C program for the "I before E except after C" rule?

To write a C program for the "I before E except after C" rule, you will need to use conditional statements and loops to check for the specific conditions of the rule. You will also need to use string manipulation functions to determine the placement of the letters "I" and "E" within a word.

Can I use regular expressions to implement the "I before E except after C" rule in my C program?

Yes, regular expressions can be used to implement the "I before E except after C" rule in a C program. However, regular expressions can be complex and may not be the most efficient solution for this particular problem.

What are some potential challenges or limitations when writing a C program for the "I before E except after C" rule?

Some potential challenges or limitations when writing a C program for the "I before E except after C" rule include handling words with multiple instances of "I" and "E", dealing with exceptions to the rule, and ensuring the program is efficient and accurate.

Are there any resources available to help me write a C program for the "I before E except after C" rule?

Yes, there are many resources available online that provide guidance and examples for writing a C program for the "I before E except after C" rule. You can also consult with other programmers or refer to programming forums for assistance.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
19
Views
964
  • Programming and Computer Science
Replies
4
Views
730
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
867
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
936
  • Programming and Computer Science
3
Replies
97
Views
7K
Back
Top