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

Here is the code for a C function that TAKES in a pointer string (characters) an

ID: 3545483 • Letter: H

Question

Here is the code for a C function that TAKES in a pointer string (characters) and calculates the length of the string... IE: String = ABCDEFG.  Length = 7.  First of all, is this the correct logic for the function?  Also, I am getting an error on the 4th line of the code, which probably has something to do with the 'for' loop.  I need to know the correct coding for this function.  Again, it takes in a character pointer and returns an integer.

int length (char *a) { int i=0; int length; for (i=0; *a != ''; i++) {length = i+1;} return length; }

Explanation / Answer

#include<iostream>

using namespace std;

void length(char *a)

{

int i=0,length=0;

for(i=1;*a!='';i++)

{

length+=1;

a=a+1;

}

cout<<length;

}

int main()

{

char s[]={"ghfdafd"};

length(s);

return 0;

}

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