K&R exercise 5-5

Discussion in 'C Programming' started by mdh, Feb 20, 2007.

  1. mdh

    mdh Guest

    /***

    I wonder if anyone can see why this is not working as planned
    ( concatenate n characters from t[] to s[]). I copy s[] to u[], then
    add n characters to []u from t[]. When I debug, I see that u[] does
    have the correct string, but when printed, it does not do so.
    Thank you.
    code below.


    ******/





    #include <stdio.h>
    # define SIZE 1000

    int main () {

    void strncat( char *s, char *t, int );
    void strcpy(char *s,char *u);

    int i=5;
    char s[]="Now is the time for all good men";
    char t[]="To be counted";
    char u[SIZE];

    strcpy(u, s);
    strncat(u, t, i);



    printf( "To the phrase: \"%s\"\n is added the first %d letters of the
    phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);
    return 0;
    }


    void strcpy(char *u, char *s){

    while ( *u++ = *s++);

    }

    void strncat( char *u, char *t, int i){

    while ( *u++);

    while ( (*u++ = *t++) && i-- > 0 );

    *u='\0';

    }
     
    mdh, Feb 20, 2007
    #1
    1. Advertising

  2. mdh

    , India Guest

    > void strncat( char *u, char *t, int i){
    >
    > while ( *u++);
    >


    Here you are going one character after the '\0' character.
    This while loop should be modified as follows:

    while (*u)
    ++u;
     
    , India, Feb 20, 2007
    #2
    1. Advertising

  3. mdh

    Klarth Guest

    I'm not 100% sure why you are redefining strcpy and strncat, but that
    is another matter. In strncat, your first while loop is searching for
    end of the string u, which is null terminated. The problem is that
    when the while loop terminates, it the u pointer has gone pass the
    original terminating null.


    On Feb 20, 1:02 pm, "mdh" <> wrote:
    > /***
    >
    > I wonder if anyone can see why this is not working as planned
    > ( concatenate n characters from t[] to s[]). I copy s[] to u[], then
    > add n characters to []u from t[]. When I debug, I see that u[] does
    > have the correct string, but when printed, it does not do so.
    > Thank you.
    > code below.
    >
    > ******/
    >
    > #include <stdio.h>
    > # define SIZE 1000
    >
    > int main () {
    >
    > void strncat( char *s, char *t, int );
    > void strcpy(char *s,char *u);
    >
    > int i=5;
    > char s[]="Now is the time for all good men";
    > char t[]="To be counted";
    > char u[SIZE];
    >
    > strcpy(u, s);
    > strncat(u, t, i);
    >
    > printf( "To the phrase: \"%s\"\n is added the first %d letters of the
    > phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);
    > return 0;
    >
    > }
    >
    > void strcpy(char *u, char *s){
    >
    > while ( *u++ = *s++);
    >
    > }
    >
    > void strncat( char *u, char *t, int i){
    >
    > while ( *u++);
    >
    > while ( (*u++ = *t++) && i-- > 0 );
    >
    > *u='\0';
    >
    > }
     
    Klarth, Feb 20, 2007
    #3
  4. mdh

    mdh Guest

    On Feb 19, 8:31 pm, "Klarth" <> wrote:
    > I'm not 100% sure why you are redefining strcpy and strncat, .......


    It's an exercise in K&R II


    > In strncat, your first while loop is searching for
    > end of the string u, which is null terminated. The problem is .......the u pointer has gone pass the
    > original terminating null.
    >



    Of course...it was there all the time, but I did not see the problem.

    Thank you.
     
    mdh, Feb 20, 2007
    #4
  5. mdh

    mdh Guest

    On Feb 19, 8:25 pm, ", India"
    <> wrote:
    > This while loop should be modified as follows:
    >
    > while (*u)
    > ++u;




    thank you.
     
    mdh, Feb 20, 2007
    #5
  6. mdh

    Default User Guest

    Klarth wrote:

    > I'm not 100% sure why you are redefining strcpy and strncat



    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or:
    <http://www.caliburn.nl/topposting.html>
     
    Default User, Feb 20, 2007
    #6
  7. On Feb 19, 11:02 pm, "mdh" <> wrote:
    > /***
    >
    > I wonder if anyone can see why this is not working as planned
    > ( concatenate n characters from t[] to s[]). I copy s[] to u[], then
    > add n characters to []u from t[]. When I debug, I see that u[] does
    > have the correct string, but when printed, it does not do so.
    > Thank you.
    > code below.
    >
    > ******/
    >
    > #include <stdio.h>
    > # define SIZE 1000
    >
    > int main () {
    >
    > void strncat( char *s, char *t, int );
    > void strcpy(char *s,char *u);
    >
    > int i=5;
    > char s[]="Now is the time for all good men";
    > char t[]="To be counted";
    > char u[SIZE];
    >
    > strcpy(u, s);
    > strncat(u, t, i);
    >
    > printf( "To the phrase: \"%s\"\n is added the first %d letters of the
    > phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);
    > return 0;
    >
    > }
    >
    > void strcpy(char *u, char *s){
    >
    > while ( *u++ = *s++);
    >
    > }
    >
    > void strncat( char *u, char *t, int i){
    >
    > while ( *u++);
    >
    > while ( (*u++ = *t++) && i-- > 0 );
    >
    > *u='\0';
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -


    Please indent the code for better readability
     
    DanielJohnson, Feb 20, 2007
    #7
  8. mdh

    santosh Guest

    mdh wrote:
    > /***
    > I wonder if anyone can see why this is not working as planned
    > ( concatenate n characters from t[] to s[]). I copy s[] to u[], then
    > add n characters to []u from t[]. When I debug, I see that u[] does
    > have the correct string, but when printed, it does not do so.
    > Thank you.
    > code below.
    > ******/
    >
    > #include <stdio.h>
    > # define SIZE 1000
    >
    > int main () {
    >
    > void strncat( char *s, char *t, int );
    > void strcpy(char *s,char *u);


    It's not a good idea to redine Standard library functions. Give them a
    name from the user's namespace.

    > int i=5;


    Why hard code this value. Just get the length of the second string
    from strlen and use that.

    > char s[]="Now is the time for all good men";
    > char t[]="To be counted";
    > char u[SIZE];
    >
    > strcpy(u, s);
    > strncat(u, t, i);
    >
    > printf( "To the phrase: \"%s\"\n is added the first %d letters of the
    > phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);


    You can't split a string like that with a direct newline. You need to
    close the previous string and begin a new string literal on the second
    line. In C, adjacent string literals are concactenated.

    Do it like:

    printf( "To the phrase: \"%s\"\n is added the first %d letters of
    the "
    "phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t,
    u);

    > return 0;
    > }
    >
    >
    > void strcpy(char *u, char *s){
    >
    > while ( *u++ = *s++);
    >
    > }
    >
    > void strncat( char *u, char *t, int i){
    >
    > while ( *u++);


    When this loop terminates u points to one past the terminating null
    character of the string. String concatenation functions must remove
    the original null character and place at the end of the expanded
    string. Here you don't do that, so printf will stop printing the
    string at this point. Just add a u--; statement below the while loop.

    > while ( (*u++ = *t++) && i-- > 0 );


    You don't need the comparision with 0. Change i to an unsigned integer
    type and it's increment in the above loop to the prefix form.

    >
    > *u='\0';


    This is not needed.

    >
    > }
     
    santosh, Feb 20, 2007
    #8
  9. mdh

    Jack Klein Guest

    On 20 Feb 2007 13:04:20 -0800, "DanielJohnson" <>
    wrote in comp.lang.c:

    > On Feb 19, 11:02 pm, "mdh" <> wrote:
    > > /***
    > >
    > > I wonder if anyone can see why this is not working as planned
    > > ( concatenate n characters from t[] to s[]). I copy s[] to u[], then
    > > add n characters to []u from t[]. When I debug, I see that u[] does
    > > have the correct string, but when printed, it does not do so.
    > > Thank you.
    > > code below.
    > >
    > > ******/
    > >
    > > #include <stdio.h>
    > > # define SIZE 1000
    > >
    > > int main () {
    > >
    > > void strncat( char *s, char *t, int );
    > > void strcpy(char *s,char *u);
    > >
    > > int i=5;
    > > char s[]="Now is the time for all good men";
    > > char t[]="To be counted";
    > > char u[SIZE];
    > >
    > > strcpy(u, s);
    > > strncat(u, t, i);
    > >
    > > printf( "To the phrase: \"%s\"\n is added the first %d letters of the
    > > phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);
    > > return 0;
    > >
    > > }
    > >
    > > void strcpy(char *u, char *s){
    > >
    > > while ( *u++ = *s++);
    > >
    > > }
    > >
    > > void strncat( char *u, char *t, int i){
    > >
    > > while ( *u++);
    > >
    > > while ( (*u++ = *t++) && i-- > 0 );
    > >
    > > *u='\0';
    > >
    > >
    > >
    > > }- Hide quoted text -
    > >
    > > - Show quoted text -

    >
    > Please indent the code for better readability


    Please learn how to post without appending those stupid strings about
    "quoted text" and making them appear to be part of the post you are
    replying to when they are not.

    If you can't figure out how to use Google groups properly, either get
    a real newsreader to post with, or don't post.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    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, Feb 21, 2007
    #9
  10. mdh

    mdh Guest

    On Feb 20, 1:08 pm, "santosh" <> wrote:
    > You don't need the comparision with 0. Change i to an unsigned integer
    > type and it's increment in the above loop to the prefix form.


    Thanks for all your input santosh. I have just "discovered" the
    usefullness of the comparison to zero. Finally just starting to enjoy
    C...very much in part to the great help provided by this forum.
     
    mdh, Feb 21, 2007
    #10
  11. mdh

    Guest

    On 20 Feb, 21:08, "santosh" <> wrote:
    > mdh wrote:

    ....

    > > void strncat( char *s, char *t, int );
    > > void strcpy(char *s,char *u);

    >
    > It's not a good idea to redine Standard library functions. Give them a
    > name from the user's namespace.


    I agree entirely! To shut up gcc I renamed them to "mystr..."

    > > int i=5;

    >
    > Why hard code this value. Just get the length of the second string
    > from strlen and use that.


    Except he might not want the whole of the second string...

    > > char s[]="Now is the time for all good men";
    > > char t[]="To be counted";
    > > char u[SIZE];


    while "i" and "j" are fair enough names for numbers used as indexes
    (old FORTRAN convention I believe), I think here we'd be better off
    with more meaningful names... It's a good habit to get into early in
    your programming life...

    > > strcpy(u, s);
    > > strncat(u, t, i);

    >
    > > printf( "To the phrase: \"%s\"\n is added the first %d letters of the
    > > phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t, u);

    >
    > You can't split a string like that with a direct newline. You need to
    > close the previous string and begin a new string literal on the second
    > line. In C, adjacent string literals are concactenated.


    I think that was just the newsreader, or posting software, wrapping...

    Just like it does with yours below :)
    >
    > Do it like:
    >
    > printf( "To the phrase: \"%s\"\n is added the first %d letters of
    > the "
    > "phrase \"%s\"\n resulting in phrase: \"%s\" \n\n", s, i ,t,
    > u);


    ....

    > > void strncat( char *u, char *t, int i){

    >
    > > while ( *u++);

    >
    > When this loop terminates u points to one past the terminating null
    > character of the string. String concatenation functions must remove
    > the original null character and place at the end of the expanded
    > string. Here you don't do that, so printf will stop printing the
    > string at this point. Just add a u--; statement below the while loop.


    An ugly suggestion, IMHO. I prefer the suggestion of

    while(*u)
    u++;

    > > while ( (*u++ = *t++) && i-- > 0 );

    >
    > You don't need the comparision with 0.


    True, but it doesn't actually do any harm and _may_ improve
    readability.

    > Change i to an unsigned integer type


    Any particular reason for that?

    > and it's increment in the above loop to the prefix form.


    > > *u='\0';

    >
    > This is not needed.


    Unless we exited the loop above due to the limit of characters copied
    rather than by
    copying the final '\0' in which case the result string is unterminated.
     
    , Feb 21, 2007
    #11
    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. Kevin Spencer
    Replies:
    2
    Views:
    454
    John Saunders
    Aug 6, 2003
  2. lonelyplanet999

    Exercise needed for java 2 programmer test

    lonelyplanet999, Sep 30, 2003, in forum: Java
    Replies:
    1
    Views:
    4,186
    VisionSet
    Sep 30, 2003
  3. Xah Lee
    Replies:
    12
    Views:
    608
    Duncan Booth
    Jun 22, 2005
  4. Bruce .J Sam
    Replies:
    0
    Views:
    1,953
    Bruce .J Sam
    Jun 16, 2005
  5. Aries
    Replies:
    7
    Views:
    405
    Aries
    May 3, 2006
Loading...

Share This Page