Pointer counting C-String

PHOTO EMBED

Tue Jun 11 2024 20:44:52 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c #function

#include <stdio.h>

int get_length(char *cstring) 
{
    int length = 0;
    while (cstring[length] != '\0') 
    {
        length++;
    }
    return length;
}

int main(void) {
    char *text[] = { "Steffan", "Pascal", "Jade" };

    printf("%s is %d chars.\n", text[0], get_length(text[0]));
    printf("%s is %d chars.\n", text[1], get_length(text[1]));
    printf("%s is %d chars.\n", text[2], get_length(text[2]));
    
    // Do not change the following code
    char user_input[64];
    scanf("%63[^\n]", user_input);
    printf("%s is %d chars.\n", user_input, get_length(user_input));
    
    return 0;
}
content_copyCOPY

The objective of this exercise is to define a function named get_length that returns the length of a given C-String (a null-terminated array of characters). The function should take a char* as a parameter and return an integer representing the string length. You're specifically asked not to use the strlen function for this implementation. Requirements Function Definition Define the get_length function that takes a char* cstring as an argument and returns an int. Main Function The main() function provides an array of C-String pointers named text. These strings will be used to test the get_length function. You should print out the length of each C-String contained in the text array by calling get_length. Additional Requirements Write your Student ID in the C comment at the top of the source code file. Ensure your code compiles and adheres to good programming standards including proper indentation, variable naming, and commenting. For example: Input Result Programming 1 Steffan is 7 chars. Pascal is 6 chars. Jade is 4 chars. Programming 1 is 13 chars.