a question about arrays and strings

B

Buck Rogers

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;
}

========================
 
J

Joona I Palaste

Buck Rogers said:
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.

#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 ([email protected]) ------------- 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
 
E

Ed Morton

Buck said:
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.
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>
 
T

Tim Goodwin

Buck Rogers said:
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.
 
C

Christopher Benson-Manica

Joona I Palaste said:
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.
 
J

Joe Wright

Buck said:
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;
}
 
M

Micah Cowan

Buck Rogers said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top