C program to find the length of a string without strlen( ) function - wordsclank.in

Sunday 11 March 2018

C program to find the length of a string without strlen( ) function

This program calculates the length  of a string manually using a for loop.We are not using strlen( ) over here.
string, strlen(), String in C



SOURCE CODE

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include
int main()
{
    char i, str[100];

    printf("Enter the string: ");
    scanf("%s", str);

    for(i = 0; str[i] != '\0'; i++);
    
    printf("Length of string: %d", i);
    return 0;
}

OUTPUT

string,string in c,strlen()

Explanation:

Here within the main() we have declared one character variable i and one character string srt[].Then we print a message for the user to input the string and stores it in str[]. Then we are running a for loop which run from i=0 until str[i]!='/0' i.e, null and it will count the i i.e, the number of elements in the string.Next we just print the value of i , i.e, the number of elements in the string.

More Topics on C program

No comments:

Post a Comment