Very proud of this code.

This is a discussion on Very proud of this code. within the C Programming forums, part of the General Programming Boards category; I know it is really nothing to some of you C-gods on the board, but i'm really proud of this ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Very proud of this code.

    I know it is really nothing to some of you C-gods on the board, but i'm really proud of this code.
    Code:
    /* find 'ei' without string functions */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void){
    
        char c,c1;
    
        while ( (c = getchar()) !='#'){
              if (c == 'e'){
                 c1 = getchar();
                 if (c1 == 'i'){
                    printf("Found ei\n");
                    }
                }
    
        }
    
         system("pause");
         return (0);
    
    
    }
    I was asked in my book to find all the occurances of 'ei' - without using pointers, arrays or even any string functions. And I have to say I am proud of my code. I am most impressed by the fact that I had to think logically. I am not very good at math and I turned to programming to help me develop my logic skills. I began programming in python which in my opinion is an unbelievable language. however the python code takes abstraction to the next level as is seen here
    Code:
    #test for ei
    word = raw_input("Enter a sentence")
    if  "ei" in word:
        print "ei is in sentence"
    else:
        print "No\n"
    Nothing wrong with python it is a RAD,versatile and flexible language. However there was not much thinking going on. With C everyithing is so basic you have to think so low level, And that is an asset. Attemping this problem even allowed me to dabble around in my IDE and though it didn't work - allowed me to tinker around with functions - which is what i will be learning next
    Code:
    /* find 'ei' without string functions very bad code cause i never debugged it */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    void check4ei(void);
    
    
    int main(void){
    
        char ch = getchar();
        int space;
    
        while ( (ch != '#') ){
              space = 0;
              check4ei()
              if ( ch == ' ' || ch == '\t' || ch =='\n' && space == 0){
                 space = 1;
                 }
    
               else{
                    continue;
                    }
    
        }
    
        system("pause");
        return (0);
    
     }
    
    
    
    
    
    
     /* function declararion */
    
    
    void check4ei(void){
    
        char c, c1;
        int eicount = 0;
        int space = 0;
    
        c = getchar();
        if (c == 'e' && space == 0){
                 c1 = getchar();
                 if (c1 == 'i' && space == 0){
                     eicount++;
                    }
    
                }
    
      }
    Anyway the program can be refactored and should. It can easily be made to find a given sequence. That is of course once user input has been validated with some C functions.
    Anyway thanks to the board for allowing me to rant and for all of your help in general when I have a probelm. I know it sounds gay, but i'm really grateful. I'm learning alot in many ways. It is 12:46 here in Canada, and I'm just so proud of myself.

    C can be annoying as F**k sometimes, but when you get it. Its really rewarding.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: Very proud of this code.

    Congrats -- your first code look good.
    The only things I can see as enhanments are:
    1) you don't need c1. After finding the 'e' you can still read the next character into c
    2) if '#' follows 'e' you code will not stop.
    3) system("pause") -- change it to cin.get(); But this will require cleaning out the cin buffer before you get to it.

    Looking at your last code though:
    Code:
    /* find 'ei' without string functions very bad code cause i never debugged it */
    It shows.

    I marked the most glaring problems I saw with a quick look. I cleaned up your formatting, too:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void check4ei(void);
    
    int main(void)
    {
        char ch = getchar();    // this is C++
        int space;
    
        // ch is never changed in this loop -- it will run forever
        while ( (ch != '#') )
        {
            space = 0;
            check4ei()
            if ( ch == ' ' || ch == '\t' || ch =='\n' && space == 0)
            {
                space = 1;
            }
            else
            {
                continue;
            }
        }
        system("pause");    // yuk
        return (0);
    }
    
    /* function declararion */
    void check4ei(void)
    {
        char c, c1;
        int eicount = 0;
        int space = 0;
    
        c = getchar();
        // space is always 0
        if (c == 'e' && space == 0)
        {
            c1 = getchar();
            if (c1 == 'i' && space == 0)
            {
                eicount++;
            }
        }
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Re: Re: Very proud of this code.

    Originally posted by WaltP
    [B]Congrats -- your first code look good.
    The only things I can see as enhanments are:
    1) you don't need c1. After finding the 'e' you can still read the next character into c
    2) if '#' follows 'e' you code will not stop.
    3) system("pause") -- change it to cin.get(); But this will require cleaning out the cin buffer before you get to it.
    Thanks for your input waltP.

    1) I see what you mean. I think I put c1 to help me visualize my logic.
    2) I could always use goto to break outta that.
    3)I'm using C. I know ppl have talked about mixing C/C++ code. is that what your talking about. or Can't i just simply use a getchar() again at the end. The reason I use system("pause"), is that sometimes getchar() doesn't work, and especially when I'm using scanf(). What are my other options.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Re: Re: Very proud of this code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void){
    
        char c;
    
        do {
              if ((c = getchar()) == 'e'){
                 if ((c = getchar()) == 'i'){
                    printf("Found ei\n");
                    }
                }
        } while ( c !='#');
    
         system("pause");
         return (0);
    }

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    Still there is problem with the code below,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
    
        char c;
    
        do {
              if ((c = getchar()) == 'e'){
                 if ((c = getchar()) == 'i'){
                    printf("Found ei\n");
                    }
                }
        } while ( c !='#');
    
         system("pause");
         return (0);
    }
    The above code does not work for the string, "eeieei" ...
    So the corrected code could be,
    Code:
    do
    {
       if((c=getchar()) == 'e')
        {
            while(c=='e')
             {
                 if((c=getchar())=='i')
                 {
                      printf("Found ei..\n");
                      break; // Just the same with or without it 
                                 // But just to avoid condition check
                 }
             }
        }
    }while(c!='#');
    Sorry if there is a mistake, don't have a compiler to check the code right now.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    I fixed the error. This program can search for any string, not just ei.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
        char c;
        char searchstr[]="ei";
        int length=strlen(searchstr);
        int index=0;
        do {
              c=getchar();
              if(c == searchstr[index]) index++;
              else if(c==searchstr[0]) index=1;
              else index=0;          
              if(index == length) {
                  printf("Found %s\n", searchstr);
                  index=0;
              }
        } while ( c !='#');
    
         system("pause");
         return (0);
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int c = 0;
    while( c != '#' )
    {
        if( (c = getchar() ) == 'e' )
            if( (c = getchar( ) ) == 'i' )
                printf("Found ei\n");
            else
                ungetc( c, stdin );
    }
    Or just do that.

    [edit]
    There are many ways to control the loop. You could put the first getchar there. You could put it before the if, you could... you get the point.
    [/edit]

    Quzah.
    Last edited by quzah; 01-21-2004 at 02:50 PM.
    Hope is the first step on the road to disappointment.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by DrZoidberg
    I fixed the error. This program can search for any string, not just ei.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
        char c;
        char searchstr[]="ei";
    Your program does not conform to the requirements of the program. They cannot use arrays or pointers.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: Re: Re: Very proud of this code.

    Originally posted by caroundw5h
    Thanks for your input waltP.
    Yer welcome

    1) I see what you mean. I think I put c1 to help me visualize my logic. makes sense
    2) I could always use goto to break outta that. Slap!!!!
    3)I'm using C. I know ppl have talked about mixing C/C++ code. is that what your talking about. or Can't i just simply use a getchar() again at the end. The reason I use system("pause"), is that sometimes getchar() doesn't work, and especially when I'm using scanf(). What are my other options. below
    getchar() is C, it's the combination that is not:
    Code:
    char ch = getchar();
    #1) in Standard C you can't define and use a variable in the same line. It should be:
    Code:
    char ch;
    ch = getchar();
    #2) getchar() returns an int, not char.
    #3) scanf() can leave crap in the input stream for the next call input to read when you want something else. Instead of scanf() use:
    Code:
    fgets(buf...);
    sscanf(buf...);
    It's easier on the headaches, and better than excedrin
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Its educational to see the different way programmers interpret a problem in their code. Thanks for the additional code guys.

    Code:
    int main(void)
    {
        char ch = getchar();    // this is C++
        int space;
    
        // ch is never changed in this loop -- it will run forever
        while ( (ch != '#') )
        {
    So is the comment style following that statement, its only only on some compilers. What i'm getting at for pedantic reasons, is assigning a function to a variable is still allowed it is more a stylistic approach in a tribute to the history of C isn't it? much like
    Code:
    int a;
    scanf(%"i" , &a);
    will still capture an integer value insted of the traditional %d formatter used.

    Let me know if that is not correct.


    3) system("pause") -- change it to cin.get(); But this will require cleaning out the cin buffer before you get to it.
    This is what i was referring to when I asked if i could mix C and C++ togather. Using cin.get(). Is that possible?


    Dr Zoidberg I'm unable to use string functions as yet in the assignement. So unfortunately, this code wouldn't be allowed
    Code:
    char searchstr[]="ei";
        int length=strlen(searchstr);

    same thing here quzah.
    Code:
    ungetc( c, stdin );
    I haven't even ran across that function yet, though I have a Idea of the codes purpose.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    Since when has
    char ch = getchar();
    been illegal in C?

    Apart from the fact that it should be
    int ch = getchar();
    seems fine to me

    Code:
    int ch, prev = 0, found = 0;
    while ( (ch=getchar()) != EOF ) {
      if ( ch == 'i' && prev == 'e' ) {
        found++;
      }
      prev = ch;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21