Discussion:
Counting Characters!!
(too old to reply)
DMZ
2024-09-14 04:59:09 UTC
Permalink
This simple program will count characters for you!!

#include <stdio.h>

int main()
{
printf(" = %d characters", printf("Hello World!"));

return 0;
}
Chris Ahlstrom
2024-09-14 19:41:37 UTC
Permalink
Post by DMZ
This simple program will count characters for you!!
#include <stdio.h>
int main()
{
printf(" = %d characters", printf("Hello World!"));
return 0;
}
Cute.
--
You can tell how far we have to go, when FORTRAN is the language of
supercomputers.
-- Steven Feiner
Mike Terry
2024-09-14 21:04:00 UTC
Permalink
Post by DMZ
This simple program will count characters for you!!
#include <stdio.h>
int main()
{
printf(" = %d characters", printf("Hello World!"));
return 0;
}
Cute.
Right - if you want a program that tells you there are 12 characters in the string "Hello world!"
this one is right up there with the others... But the technique used doesn't always work - TAKE
CARE when adapting the code for your own programs!

Mike.
Bonita Montero
2024-09-16 15:55:02 UTC
Permalink
Post by DMZ
This simple program will count characters for you!!
#include <stdio.h>
int main()
{
printf(" = %d characters", printf("Hello World!"));
return 0;
}
Cute.
Nice, but this counts the characters at compile-time:

std::string_view sv( "Hello World!" );
std::cout << sv << " = " << sv.length() << " characters" << endl;
M***@dastardlyhq.com
2024-09-16 16:08:14 UTC
Permalink
On Mon, 16 Sep 2024 17:55:02 +0200
Post by Bonita Montero
Post by DMZ
This simple program will count characters for you!!
#include <stdio.h>
int main()
{
printf(" = %d characters", printf("Hello World!"));
return 0;
}
Cute.
std::string_view sv( "Hello World!" );
std::cout << sv << " = " << sv.length() << " characters" << endl;
So does sizeof("Hello world!");
Bonita Montero
2024-09-16 17:08:14 UTC
Permalink
Post by M***@dastardlyhq.com
On Mon, 16 Sep 2024 17:55:02 +0200
Post by Bonita Montero
Post by DMZ
This simple program will count characters for you!!
#include <stdio.h>
int main()
{
printf(" = %d characters", printf("Hello World!"));
return 0;
}
Cute.
std::string_view sv( "Hello World!" );
std::cout << sv << " = " << sv.length() << " characters" << endl;
So does sizeof("Hello world!");
But sizeof(string_literal) includes the null-terminating character.
M***@dastardlyhq.com
2024-09-17 07:21:37 UTC
Permalink
On Mon, 16 Sep 2024 19:08:14 +0200
Post by Bonita Montero
Post by M***@dastardlyhq.com
On Mon, 16 Sep 2024 17:55:02 +0200
Post by Bonita Montero
Post by DMZ
This simple program will count characters for you!!
#include <stdio.h>
int main()
{
printf(" = %d characters", printf("Hello World!"));
return 0;
}
Cute.
std::string_view sv( "Hello World!" );
std::cout << sv << " = " << sv.length() << " characters" << endl;
So does sizeof("Hello world!");
But sizeof(string_literal) includes the null-terminating character.
Gosh! Maybe you could do sizeof()-1 then. Just a thought!
Bonita Montero
2024-09-17 11:41:06 UTC
Permalink
Post by M***@dastardlyhq.com
On Mon, 16 Sep 2024 19:08:14 +0200
Post by Bonita Montero
Post by M***@dastardlyhq.com
On Mon, 16 Sep 2024 17:55:02 +0200
Post by Bonita Montero
Post by DMZ
This simple program will count characters for you!!
#include <stdio.h>
int main()
{
printf(" = %d characters", printf("Hello World!"));
return 0;
}
Cute.
std::string_view sv( "Hello World!" );
std::cout << sv << " = " << sv.length() << " characters" << endl;
So does sizeof("Hello world!");
But sizeof(string_literal) includes the null-terminating character.
Gosh! Maybe you could do sizeof()-1 then. Just a thought!
How about this:

template<size_t N>
consteval size_t xstrlen( char const (&x)[N] )
{
return N - !x[N - 1];
}

Loading...