C

C Program To Find The Length Of A String Without Using The Strlen Function

The following C program counts the characters in a given String to determine and display its length on the console. When this program is run, the user is prompted to enter a string, after which the program counts the characters and displays the length of the input string as output.

Finding the length of a String in C without using the standard library function strlen

/* C Program to find the length of a String without
 *  using any standard library function 
 */
#include <stdio.h>
int main()
{
    /* Here we are taking a char array of size 
     * 100 which means this array can hold a string 
     * of 100 chars. You can change this as per requirement
     */
    char str[100],i;
    printf("Enter a string: \n");
    scanf("%s",str);

    // '\0' represents end of String
    for(i=0; str[i]!='\0'; ++i);
       printf("\nLength of input string: %d",i);
    
    return 0;
}

The following C program counts the characters in a given String to determine and display its length on the console. When this program is run, the user is prompted to enter a string, after which the program counts the characters and displays the length of the input string as output.
Finding the length of a String in C without using the standard library function strlen

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button