how to find length of a character string without using while loop or user defined function?

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

Have you tried Google... but these programming problems are pure tripe in the real world.

without using while loop or user defined function?

with this line , your question becomes very easy.I'd say use library function.I think its str.length();

There are C strings (char*) and C++ strings (std::string). Which are you using?

For C-Strings you could take a look at the cstring library, which contains a function named strlen. While you're not looking for a user-defined function, you could also define it yourself as it's quite straight forward. It'd look similar to this:

constexpr size_t strlen (const char text[])
{
    // Note: text[0] checks text[0] != '\0'
    return (text[0]) ? 1 + strlen(text + 1) : 0;
}

For string objects you would use the length member function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.