Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a question about arrays and strings

Reply
Thread Tools

a question about arrays and strings

 
 
Buck Rogers
Guest
Posts: n/a
 
      10-22-2003
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;
}

========================




 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      10-22-2003
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
 
Reply With Quote
 
 
 
 
Ed Morton
Guest
Posts: n/a
 
      10-22-2003


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>

 
Reply With Quote
 
Tim Goodwin
Guest
Posts: n/a
 
      10-22-2003
"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
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-22-2003
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.
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      10-22-2003
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 ---
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      10-22-2003
"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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between << and += for Strings and Arrays. Bug? Pieter Hugo Ruby 13 11-26-2009 10:16 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
A simple newbie question (arrays and strings) koichirose Ruby 13 05-27-2008 05:39 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Beginner: A question about strings/arrays Tarjei Romtveit C++ 5 08-21-2005 11:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57