Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Show the internal representation of strl (in the same fashion as you did in the

ID: 3767101 • Letter: S

Question

Show the internal representation of strl (in the same fashion as you did in the first question) and the value of the variable length after each of the following statements, given the declarations: strcpy(str1, "Joe"); length = strlen(str1); Suppose the C++ function strlen() didn't already exist. Write code to implement your version of the strlen() function. That is, write a function to determine the length of a C string, str, supplied as an argument. Call your function MyStrLen() The function prototype for your function is: int MyStrLen (char str [ ] ) Write a mam program to test your function. The main program should contain a sentinel-controlled loop that Reads a C string, Finds the length of the C string using your MyStrLen() function, Prints the length of the C string. The loop should terminate when the sentinel string END is read. Remember to use the function strcmp() to test for the terminating condition.

Explanation / Answer

3. The internal representation:

1. strcpy(str1, "Joe");

2. length = strlen(str1); //Here the length is assigned with a value of 3. Note that is the delimiter, and it will not be counted as length.

3. strcpy(str1, "Joseph");

* *

Note that, in C++, when the string size goes out of bounds, the behaviour is UNDEFINED. Which means, it may work properly, it may not work properly, and its machine and its internal behaviour dependent. So, there is no guarantee on how it will be represented beyond your array range. But with reasonably small size going beyond the array size, most of the times, it may work properly, but don't take it as granted, and most importantly, it should be handled by the developer, and should not be left to the processor.

4. length = strlen(str1); //With the explanation given in question (3), you can conclude that this value is also undefined.

4. Suppose the C++ function strlen() didn't already exist.

Here is the code for you.

int MyStrLen(char str[])
{
int len = 0;
for(; str[len] != ''; len++);
return len;
}
int main()
{
char str1[50] = " ";   //Declares a character array of size 50.
int length;       //Declares an integer variable length.  
while(strcmp(str1, "END"))
{
cout<<"Enter the string: ";
cin>>str1;
if(!strcmp(str1, "END"))
break;
length = MyStrLen(str1);
cout<<"Length using MyStrLen of: "<<str1<<" is: "<<length<<endl;
}
}

If you need any further clarifications, just get back to me.

0 1 2 3 4 J o e
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote