a question about arrays and strings

Discussion in 'C Programming' started by Buck Rogers, Oct 22, 2003.

  1. Buck Rogers

    Buck Rogers Guest

    Hi guys, newbie here.

    I am trying to write a program which counts the number of characters
    in two strings, then prints the number, and states which string is longer.

    For some reason, my program gives an incorrect number of characters
    in each string(1 character less), and I have no idea why.

    Can somebody pls explain this?

    Thanks in advance.

    Buck.
    ..
    =================

    #include <stdio.h>

    char string_one[] = "string one";
    char string_two[] = "is this string longer?";

    int main ( void )
    {
    int ctr, ctr1, ctr2;

    for( ctr = 0; string_one[ctr] != NULL; ctr++ )
    {
    ctr1 = ctr;
    }

    for( ctr = 0; string_two[ctr] != NULL; ctr++ )
    {
    ctr2 = ctr;
    }

    printf(" \nstring_one has %d characters\n ", ctr1);
    printf(" \nstring_two has %d characters\n ", ctr2);

    if( ctr1 < ctr2 )
    {
    printf( "The longer string is string_two" );
    }

    else
    printf( "\nstring_one is longer" );

    return 0;
    }

    ========================
     
    Buck Rogers, Oct 22, 2003
    #1
    1. Advertising

  2. Buck Rogers <> scribbled the following:
    > Hi guys, newbie here.


    > I am trying to write a program which counts the number of characters
    > in two strings, then prints the number, and states which string is longer.


    > For some reason, my program gives an incorrect number of characters
    > in each string(1 character less), and I have no idea why.


    > Can somebody pls explain this?


    > Thanks in advance.


    > Buck.
    > .
    > =================


    > #include <stdio.h>


    > char string_one[] = "string one";
    > char string_two[] = "is this string longer?";


    > int main ( void )
    > {
    > int ctr, ctr1, ctr2;


    > for( ctr = 0; string_one[ctr] != NULL; ctr++ )
    > {
    > ctr1 = ctr;
    > }


    The fault is in this loop. If string_one[ctr] == NULL, ctr will be
    holding the number of non-NULL characters in string_one, because ctr
    starts at 0 and C arrays are 0-based. But the body of the loop will
    not execute, so ctr1 will still hold the old value, equal to the
    number of non-NULL characters in string_one minus one.
    Solution: Move the assignment ctr1 = ctr; outside the loop.

    > for( ctr = 0; string_two[ctr] != NULL; ctr++ )
    > {
    > ctr2 = ctr;
    > }


    Likewise here.

    > printf(" \nstring_one has %d characters\n ", ctr1);
    > printf(" \nstring_two has %d characters\n ", ctr2);


    > if( ctr1 < ctr2 )
    > {
    > printf( "The longer string is string_two" );
    > }


    > else
    > printf( "\nstring_one is longer" );


    > return 0;
    > }


    > ========================


    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "You have moved your mouse, for these changes to take effect you must shut down
    and restart your computer. Do you want to restart your computer now?"
    - Karri Kalpio
     
    Joona I Palaste, Oct 22, 2003
    #2
    1. Advertising

  3. Buck Rogers

    Ed Morton Guest

    Buck Rogers wrote:

    > Hi guys, newbie here.
    >
    > I am trying to write a program which counts the number of characters
    > in two strings, then prints the number, and states which string is longer.
    >
    > For some reason, my program gives an incorrect number of characters
    > in each string(1 character less), and I have no idea why.


    Do you want to count the nul character or not? i.e. is "hello" 5
    characters or 6?

    > Can somebody pls explain this?
    >
    > Thanks in advance.
    >
    > Buck.
    > .
    > =================
    >
    > #include <stdio.h>
    >
    > char string_one[] = "string one";
    > char string_two[] = "is this string longer?";
    >
    > int main ( void )
    > {
    > int ctr, ctr1, ctr2;
    >
    > for( ctr = 0; string_one[ctr] != NULL; ctr++ )


    NULL is a pointer. Strings end with the nul character '\0'. You could
    just drop the "!= ..." part and it'd work.

    > {
    > ctr1 = ctr;
    > }


    You COULD just set ctr1 = ctr (or ctr++ if you want to count the nul
    char too) once outside the loop y'know.

    > for( ctr = 0; string_two[ctr] != NULL; ctr++ )
    > {
    > ctr2 = ctr;
    > }


    ditto.
    <snip>
    > printf( "\nstring_one is longer" );


    No big deal but FYI you could've use "puts" instead of "printf" for
    those printfs that don't print data. If you do use "printf()", put the
    "\n"s at the end of each string.

    By the way, there is a "strlen()" function that returns the length of a
    string. See http://www-ccs.ucsd.edu/c/string.html#strlen for details.

    Ed.

    <snip>
     
    Ed Morton, Oct 22, 2003
    #3
  4. Buck Rogers

    Tim Goodwin Guest

    "Buck Rogers" <> writes:

    > For some reason, my program gives an incorrect number of characters
    > in each string(1 character less), and I have no idea why.


    > for( ctr = 0; string_one[ctr] != NULL; ctr++ )


    C strings are terminated by the character '\0'. This is sometimes
    called "the null character", or even ASCII NUL (with but a single
    letter ell!).

    You have used the NULL macro, which is one way to write a null
    pointer. Despite the similar names, they are completely different
    things (although depending on your system, it may happen that you can
    confuse them, and things happen still to work). Forget about the NULL
    macro till you've covered pointers (and then make sure you read
    section 5 of the comp.lang.c FAQ).

    Fixing this problem, and using a more conventional layout (I bet
    neither your instructor nor your textbook indents by just one space!),
    we get this:

    for (ctr = 0; string_one[ctr] != '\0'; ctr++) {
    ctr1 = ctr;
    }

    After the loop, what is the value of ctr? And ctr1? Work it through
    by hand, paying special attention to the last iteration of the loop.

    If I change your program so that string_one is empty...

    char string_one[] = "";

    then the program tells me:

    string_one has 582 characters

    How can that be?!?

    You might also like to try converting the for loop into a while loop.

    Tim.
    --
    Tim Goodwin | "But fixing the entire UNIX system is not
    Leicester, UK | on my TODO list. (Yet.)" -- Dan Bernstein
     
    Tim Goodwin, Oct 22, 2003
    #4
  5. Joona I Palaste <> spoke thus:

    > The fault is in this loop. If string_one[ctr] == NULL, ctr will be
    > holding the number of non-NULL characters in string_one, because ctr
    > starts at 0 and C arrays are 0-based. But the body of the loop will
    > not execute, so ctr1 will still hold the old value, equal to the
    > number of non-NULL characters in string_one minus one.
    > Solution: Move the assignment ctr1 = ctr; outside the loop.


    Actually, ctr1 is equal to the index of the last non-null character in the
    string - the loop executes the correct number of times, but for obvious
    reasons the index of the last non-null character is one less than the actual
    string length.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 22, 2003
    #5
  6. Buck Rogers

    Joe Wright Guest

    Buck Rogers wrote:
    >
    > Hi guys, newbie here.
    >
    > I am trying to write a program which counts the number of characters
    > in two strings, then prints the number, and states which string is longer.
    >
    > For some reason, my program gives an incorrect number of characters
    > in each string(1 character less), and I have no idea why.
    >
    > Can somebody pls explain this?
    >
    > Thanks in advance.
    >
    > Buck.
    > .
    > =================
    >
    > #include <stdio.h>
    >
    > char string_one[] = "string one";
    > char string_two[] = "is this string longer?";
    >
    > int main ( void )
    > {
    > int ctr, ctr1, ctr2;
    >
    > for( ctr = 0; string_one[ctr] != NULL; ctr++ )
    > {
    > ctr1 = ctr;
    > }
    >
    > for( ctr = 0; string_two[ctr] != NULL; ctr++ )
    > {
    > ctr2 = ctr;
    > }
    >
    > printf(" \nstring_one has %d characters\n ", ctr1);
    > printf(" \nstring_two has %d characters\n ", ctr2);
    >
    > if( ctr1 < ctr2 )
    > {
    > printf( "The longer string is string_two" );
    > }
    >
    > else
    > printf( "\nstring_one is longer" );
    >
    > return 0;
    > }
    >
    > ========================


    I fixed it. See if you can figure it out.

    #include <stdio.h>

    char string_one[] = "string one";
    char string_two[] = "is this string longer?";

    int main(void)
    {
    int ctr, ctr1, ctr2;
    for (ctr = 0; string_one[ctr] != '\0'; ctr++)
    ;
    ctr1 = ctr;

    for (ctr = 0; string_two[ctr] != '\0'; ctr++)
    ;
    ctr2 = ctr;

    printf("string_one has %d characters\n", ctr1);
    printf("string_two has %d characters\n", ctr2);

    if (ctr1 < ctr2)
    printf("The longer string is string_two");
    else
    printf("string_one is longer");

    return 0;
    }

    --
    Joe Wright http://www.jw-wright.com
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Oct 22, 2003
    #6
  7. Buck Rogers

    Micah Cowan Guest

    "Buck Rogers" <> writes:

    > Hi guys, newbie here.
    >
    > I am trying to write a program which counts the number of characters
    > in two strings, then prints the number, and states which string is longer.
    >
    > For some reason, my program gives an incorrect number of characters
    > in each string(1 character less), and I have no idea why.
    >
    > Can somebody pls explain this?
    >
    > Thanks in advance.
    >
    > Buck.
    > .
    > =================
    >
    > #include <stdio.h>
    >
    > char string_one[] = "string one";
    > char string_two[] = "is this string longer?";
    >
    > int main ( void )
    > {
    > int ctr, ctr1, ctr2;
    >
    > for( ctr = 0; string_one[ctr] != NULL; ctr++ )


    The comparison against NULL is strange. NULL is meant to represent a
    null pointer constant, not some type of character. If NULL is
    defined as (void*)0, then string_one[ctr] will be converted to a
    void* before the comparison, which could result in a trap
    representation; or conceivably even a false positive for your
    comparison. NULL is not meant to represent the "null character
    terminator", which is simply a zero-valued integer. Use one of:

    string_one[ctr] != 0
    string_one[ctr] != '\0'

    (Both have exactly the same meaning, but I find the first
    slightly clearer).

    > {
    > ctr1 = ctr;


    You know, you could avoid this assignment if you just used ctr1
    instead of ctr within the for clauses... At any rate, assigning
    to ctr1 on every iteration is just wasted cycles: and it also
    accounts for the error in your algorithm. Because when ctr *is*
    the right value for the length, the body of the for-loop never
    gets executed, since (string_one[ctr] != '\0') is true.

    > }


    Why did you feel the need to write this yourself? The standard
    library already provides strlen() in string.h.

    >
    > for( ctr = 0; string_two[ctr] != NULL; ctr++ )
    > {
    > ctr2 = ctr;
    > }


    Same comments as above.

    >
    > printf(" \nstring_one has %d characters\n ", ctr1);
    > printf(" \nstring_two has %d characters\n ", ctr2);
    >
    > if( ctr1 < ctr2 )
    > {
    > printf( "The longer string is string_two" );
    > }
    >
    > else
    > printf( "\nstring_one is longer" );
    >
    > return 0;
    > }


    Both of the conditionally executed printf() statements above
    don't end with a line-terminating '\n', as they should. I find
    that it's much easier to realize this sort of problem if you make
    the general habit of putting your '\n's at the *end* of the
    strings you output, rather than at the beginning (I've *never*
    understood why so many beginners do this. Is there a book
    somewhere that advocates this?).

    HAND.

    --
    Micah J. Cowan
     
    Micah Cowan, Oct 22, 2003
    #7
    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. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    798
    Malcolm
    Jun 24, 2006
  2. Tarjei Romtveit
    Replies:
    5
    Views:
    334
    Jim Langston
    Aug 22, 2005
  3. Philipp
    Replies:
    21
    Views:
    1,167
    Philipp
    Jan 20, 2009
  4. koichirose
    Replies:
    13
    Views:
    224
    Todd Benson
    May 27, 2008
  5. Pieter Hugo
    Replies:
    13
    Views:
    215
    Robert Klemme
    Nov 26, 2009
Loading...

Share This Page