Find String Length without using Predefined Function in C
The program calculates and prints the length of a given string.
- It starts by declaring the variables i and c (a counter).
- Then it takes a string input from the user using gets() and stores it in the str array.
- The program prints the given string using puts(str).
- A for loop is used to iterate over the string and count the number of characters in it. str[i]!='\0' checks if the current character is not the null terminator, indicating the end of the string.
- The counter c is incremented for each character.
- Finally, the length of the string is printed using printf("\nString Length:%d",c);
- The program returns 0 to indicate successful execution.
Source Code
#include<stdio.h>
int main()
{
int i,c=0;
char str[100];
printf("\nEnter the String:");
gets(str);
printf("\nGiven String:");
puts(str);
for(i=0;str[i]!='\0';i++)
{
c++;
}
printf("\nString Length:%d",c);
return 0;
}
To download raw file
Click Here
Output
Enter the String:Tutor Joe's
string length:11