Is my code good?

Discussion in 'C Programming' started by poison.summer@gmail.com, Nov 30, 2005.

  1. Guest

    Here is my short code to implement such function:
    Check whether the string starts with "12", if not
    add "12" in front of the string.

    Is the code good? or any better way to do it?

    Thanks a lot!

    #include <string.h>
    int main()
    {
    char A[10];
    char B[10];
    strcpy(A,"abcdef\0");
    printf("A=(%s)\n",A);
    if((A[0]=='1')&&(A[1]=='2'))
    printf("A=(%s)\n",A);
    else
    {
    B[0]='1';
    B[1]='2';
    B[2]='\0';
    strcat(B,A);
    printf("B=(%s)",B);
    }
    }
    ~
    ~
     
    , Nov 30, 2005
    #1
    1. Advertising

  2. Skarmander Guest

    wrote:
    > Here is my short code to implement such function:
    > Check whether the string starts with "12", if not
    > add "12" in front of the string.
    >
    > Is the code good? or any better way to do it?
    >
    > Thanks a lot!
    >
    > #include <string.h>
    > int main()
    > {
    > char A[10];
    > char B[10];


    Statically allocated buffers are easy to work with, but they shouldn't be
    used if there's no practical limit for your input size. A string could be of
    any length.

    > strcpy(A,"abcdef\0");


    The string literal automatically ends in a NUL. You don't need to add one
    yourself. "abcdef" will do fine.

    > printf("A=(%s)\n",A);


    You forgot to #include <stdio.h>. If your compiler didn't warn you about
    this, kick it. (Or just ask it to produce more warnings.)

    > if((A[0]=='1')&&(A[1]=='2'))
    > printf("A=(%s)\n",A);
    > else
    > {
    > B[0]='1';
    > B[1]='2';
    > B[2]='\0';
    > strcat(B,A);
    > printf("B=(%s)",B);
    > }
    > }


    Your code "works", as long as the "strings" are always within bounds, but
    from the way you describe the problem, you are probably supposed to
    implement a function that looks like this:

    void ensure_12_prefix(char* s) { ... }

    Where s is a string with enough room to add "12" in front of it if it's not
    already there. Try your hand at that one.

    Of course, I'm just guessing as to the actual intent of the assignment. It's
    a bit vague. "The string"? What string?

    S.
     
    Skarmander, Nov 30, 2005
    #2
    1. Advertising

  3. On 30 Nov 2005 14:36:17 -0800, in comp.lang.c ,
    wrote:

    >Here is my short code to implement such function:
    >Check whether the string starts with "12", if not
    >add "12" in front of the string.
    >
    >Is the code good? or any better way to do it?



    use strncmp()

    // assumes sensible definitions of a and b
    if(strncmp(a, "12",2)
    sprintf(b, "12%s", a);

    > char A[10];
    > char B[10];


    Bad style: upper-case identifiers are generally reserved for Macros -
    variables should be lowercase or mixed case.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Nov 30, 2005
    #3
  4. Randy Howard Guest

    wrote
    (in article
    <>):

    > Here is my short code to implement such function:


    Actually, it does it all in main, but it does 'function' in the
    sense of sending stuff to stdout I suppose. If it would compile
    that is. I suspect it is probably supposed to take a string
    passed on the command line (or be an actual function that works
    on a string passed in) before you turn it in.

    > Check whether the string starts with "12", if not
    > add "12" in front of the string.
    >
    > Is the code good? or any better way to do it?


    How many days until your homework is due?

    > Thanks a lot!
    >
    > #include <string.h>

    You left out <stdio.h>, which is needed for printf.

    > int main()

    try int main(void)
    > {
    > char A[10];
    > char B[10];
    > strcpy(A,"abcdef\0");


    Google for 'buffer overflow'. This program has it written all
    over it, especially if you make it more general. Also, think
    about

    > printf("A=(%s)\n",A);
    > if((A[0]=='1')&&(A[1]=='2'))
    > printf("A=(%s)\n",A);
    > else
    > {
    > B[0]='1';
    > B[1]='2';
    > B[2]='\0';
    > strcat(B,A);
    > printf("B=(%s)",B);
    > }

    You need either
    return 0;
    or
    return EXIT_SUCCESS; /* and stlib.h */
    here.
    > }


    You also need to take a long look at how this works (or does
    not) work when you use input other than 'abcdef'.

    If you make this into a more general function to prefix strings,
    you're going to have to be much more careful.



    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
     
    Randy Howard, Nov 30, 2005
    #4
  5. Jack Klein Guest

    On Wed, 30 Nov 2005 23:52:52 +0100, Skarmander
    <> wrote in comp.lang.c:

    > wrote:
    > > Here is my short code to implement such function:
    > > Check whether the string starts with "12", if not
    > > add "12" in front of the string.
    > >
    > > Is the code good? or any better way to do it?
    > >
    > > Thanks a lot!
    > >
    > > #include <string.h>
    > > int main()
    > > {
    > > char A[10];
    > > char B[10];

    >
    > Statically allocated buffers are easy to work with, but they shouldn't be
    > used if there's no practical limit for your input size. A string could be of
    > any length.


    This is the kind of improper terminology that confuses newbies. The
    two arrays you refer to are most certainly not 'statically allocated',
    which would mean, in C, that they have static storage duration.

    These arrays have automatic storage class and duration.

    Please refer to this as "fixed size", and don't confuse newbies.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 1, 2005
    #5
  6. Michael Mair Guest

    wrote:
    > Here is my short code to implement such function:
    > Check whether the string starts with "12", if not
    > add "12" in front of the string.
    >
    > Is the code good? or any better way to do it?
    >
    > Thanks a lot!
    >
    > #include <string.h>
    > int main()
    > {
    > char A[10];
    > char B[10];
    > strcpy(A,"abcdef\0");
    > printf("A=(%s)\n",A);
    > if((A[0]=='1')&&(A[1]=='2'))
    > printf("A=(%s)\n",A);
    > else
    > {
    > B[0]='1';
    > B[1]='2';
    > B[2]='\0';
    > strcat(B,A);
    > printf("B=(%s)",B);


    In addition to what the other answers noted:
    You need a '\n' at the end of the last output if you
    want to be sure you actually get it out.

    -Michael
    > }
    > }
    > ~
    > ~
    >



    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Dec 1, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sikka noel
    Replies:
    8
    Views:
    422
    Mike Wahler
    Aug 5, 2003
  2. vlsidesign
    Replies:
    26
    Views:
    992
    Keith Thompson
    Jan 2, 2007
  3. Cliff  Martin
    Replies:
    1
    Views:
    3,043
    Larry Smith
    Jan 31, 2007
  4. SM
    Replies:
    9
    Views:
    509
  5. Casey Hawthorne
    Replies:
    18
    Views:
    631
    Beej Jorgensen
    Nov 6, 2009
Loading...

Share This Page