You are here:

C++/counting the strings


good day,pls help me with my problem in c++

i'm currently making a program on how to count the length of the string defined to be number of characters.And it needs to assign a function that each has an assigned string. the two strings are then compared. string with greater length will be displayed first and so with its length value; followed by the string with less length value. use functions, such as assign(), that stores a character string into the hidden variables (Hint: use private class). And the function length(), that returns the length of the string. use also public class so that two strings in diffirent functions will be counted.

pls help.

I don't fully understand what you are asking for, so if I do not answer your question in full, please feel free to send a followup question.

I'll start with a brief overview of how character strings work.  Character strings are composed of some number of printable character and terminate with a special character called the NULL terminator.  The NULL terminator has a value of 0, or can also be represented as '\0'.  When printing a character string out, the print functions essentially loop through the string, displaying every character until they reach the special character, the NULL terminator.

So, what that in mind, let's examine how we would determine the length of a string.

Essentially, determining the length of a string requires the same overall logic as printing out a string, except you don't actually print the character out.  You still need to go through each index in the character array until you reach the NULL terminator.  This function would look something like this:

   int length(const char* _szStr)
       int nLength = 0;

       if (_szStr)
         char* pCurPos = (char*)_szStr;
         while (*pCurPos)

       return nLength;

What this does is simply go character-by-character through the string, using the value of the character as a boolean value (0 is false, and the NULL terminator is 0).  Once the while loop reaches the NULL terminator, the loop will exit and nLength will be equal to the number of printable characters in the string.  Let's look at a simple driver program for it:

   void main()
       char* pStr = "This is my String.";
       int len = length(pStr);

The variable "len" will equal 18 at the end of this application.  It is important to note that while there are 18 printable characters in the string, the string actually contains 19 characters, since the NULL terminator must exist at the end of the string to terminate the string.  If you intend to copy the string to a new location, you *must* add space for the NULL terminator.

As I stated at the beginning of this answer, if I have left any questions unanswered or if I have raised new questions, please feel free to ask a followup question and I will answer that, as well.


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2017 All rights reserved.