Write a program to find the length of the string in C.
Approach 1: Without using strlen().
C Program
#include <stdio.h>#include <string.h>int main(){char str[1001];printf("Enter a string : ");fgets(str, sizeof(str), stdin);int length = 0;while (str[length] != '\0'){length++;}printf("Length of string is %d ", length);return 0;}
Approach 2: Using strlen()
C Program
#include <stdio.h>#include <string.h>int main(){char str[1000];printf("Enter a string : ");fgets(str, sizeof(str), stdin);int length = strlen(str);printf("Length of string is %d", length);return 0;}
Input:
Enter a string : Coding is Fun
Output:
Length of string is 14
No comments:
Post a Comment