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

Using C++ and \"C\" STRINGS The standard library function strlen() (found in str

ID: 3856858 • Letter: U

Question

Using C++ and "C" STRINGS The standard library function strlen() (found in string.h) returns the number of characters in a string, not including the terminating NUL ('') character. Write your own StringLength() function that does exactly the same thing. Write a short main() program to declare and initialize a string. Pass the string to StringLength() to demonstrate that it can return an integer value representing the number of characters in the test string. Use a cout statement in main() to print out the length of the string. Please note: Your function should count the characters in the string, not access some C++ library function or method. DO NOT #include or . There should be no console input or output in the function. All input/output should be in main().

Explanation / Answer

Given below is the code for the question. Please rate the answer if it helped. Thank you.

#include <iostream>

using namespace std;

//function to calculate the length of a string

int StringLength(char *str);

int main()

{

char str[20] = "Good Morning";

cout << "The length of "" << str << "" is " << StringLength(str) << endl;

return 0;

}

int StringLength(char *str)

{

int count = 0;

while(*str != '')

{

count++;

str++;

}

return count;

}

output

The length of "Good Morning" is 12

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