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