Sunday, 31 August 2014

C Function to find out the length of a string

This is a function written in C language to find out the length of a string given by the user.


Source Code

# include <stdio.h>
# include <conio.h>

/* Function definition */
int lenstr (char str [] ) {
    int len=0;
    while (str[len++]!=0);
    len--;
    return len;
}




int main() {
    char str[30];
    int len=0;

    clrscr();

    printf("Enter a string:");
    scanf("%s", str);
    len=lenstr(str); // Function Call
    printf("\n\nLength of the string: %d", len);

    getch();
    return 0;
}



Output


No comments:

Post a Comment