Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Writing a program in C

  1. Oct 24, 2009 #1
    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.
     
  2. jcsd
  3. Oct 24, 2009 #2

    sylas

    User Avatar
    Science Advisor

    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
     
  4. Oct 24, 2009 #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.
     
  5. Oct 24, 2009 #4

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  6. Oct 24, 2009 #5

    sylas

    User Avatar
    Science Advisor

    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 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 (Text):

    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 (Text):

        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: Oct 24, 2009
  7. Oct 24, 2009 #6

    Mark44

    Staff: Mentor

    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 (Text):

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

    z = x & y;
    printf("z is %d", z);
     
     
  8. Oct 25, 2009 #7

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

    sylas

    User Avatar
    Science Advisor

    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.
     
  10. Oct 25, 2009 #9

    Borek

    User Avatar

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook