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.
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;
char* pCurPos = (char*)_szStr;
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:
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.