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

Arrays and Pointers CSE 202 Lab 3: Arrays and Pointers . Perform the following e

ID: 3713796 • Letter: A

Question

Arrays and Pointers

CSE 202 Lab 3: Arrays and Pointers . Perform the following exercises under lab3 sub directory. 2. Here is strlen) function which returns the length of a null-terminated character-array unsigned int strlen (const char s) unsigned int n; for (n - O; s[n]; n++); // same as s[n] 1. ".O return ni '10 Here is the same function using pointers: unsigned int strlen(const char unsigned int n for (n -0(s+n)i n++); return n; Write your own main() to test the correctness of both versions of this function

Explanation / Answer

PLEASE REFER BELOW CODE

#include<stdlib.h>
#include<stdio.h>

unsigned int strlen_arr(const char s[])
{
unsigned int n;
for(n = 0; s[n]; n++);

return n;
}
unsigned int strlen_ptr(const char *s)
{
unsigned int n;
for(n = 0; s[n]; n++);

return n;
}
void strcpy_arr(char t[], const char s[])
{
int i;
for(i=0; t[i] = s[i]; i++);
}
void strcpy_ptr(char *t, const char *s)
{
for(; *t++ = *s++;);
}
int main()
{
char str_arr[100] = "Chegg";
char *str_ptr = "Chegg";
char str_dest_arr[100];
char *str_dest_ptr;
unsigned int p,q;
p = strlen_arr(str_arr);
q = strlen_ptr(str_ptr);
printf("Length with array = %u Length with pointer=%u ", p,q);

strcpy_arr(str_dest_arr,str_arr);
printf("With array = %s ", str_dest_arr);
str_dest_ptr = (char *)malloc(sizeof(char) * p);
strcpy_ptr(str_dest_ptr,str_ptr);
printf("With pointer = %s ", str_dest_ptr);
return 0;
}

PLEASE REFER BELOW OUTPUT

Length with array = 5
Length with pointer=5
With array = Chegg
With pointer = Chegg

Process returned 0 (0x0) execution time : 0.044 s
Press any key to continue.

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