Q:

C program to find Length of the String by passing String/ Character Array as an Argument using User Define Functions

0

C program to find Length of the String by passing String/ Character Array as an Argument using User Define Functions

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C program to find Length of the String by passing String/ Character Array as an Argument using User Define Functions

/*  
C program to find Length of the String by passing String/ Character Array 
as an argument using User Define Functions.
*/

#include <stdio.h>

/*function declaration*/
int stringLength(char*);

int main()
{
    char text[100];
    int length;

    printf("Enter text (max- 100 characters): ");
    scanf("%[^\n]s", text);
    /*we can also use gets(text) - To read complete text untill '\n'*/

    length = stringLength(text);

    printf("Input text is: %s\n", text);
    printf("Length is: %d\n", length);

    return 0;
}

/* function definition...
 * Function     :   stringLength
 * Argument(s)  :   char * - Pointer of character arrar
 * Return Type  :   int - Length of the string (integer type)
*/
int stringLength(char* str)
{
    int len = 0;

    /*calculate string length*/
    for (len = 0; str[len] != '\0'; len++)
        ;

    /*return len*/
    return len;
}

Output:

    Enter text (max- 100 characters): Hello World.
    Input text is: Hello World.
    Length is: 12

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now