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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.