This is a C program to find the length of a string without using the built-in function.
This program takes a string as input and finds its length without using the built-in function.
1. Take a string as input and store it in the array.
2. Using for loop count the number of characters in the array and store the result in a variable.
3. Print the variable as output.
Here is source code of the C program to find the length of a string without using the built-in function.The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to find the length of a string without using the
* built-in function
*/
#include <stdio.h>
void main()
{
char string[50];
int i, length = 0;
printf("Enter a string \n");
gets(string);
/* keep going through each character of the string till its end */
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of a string is the number of characters in it \n");
printf("So, the length of %s = %d\n", string, length);
}
1. Take a string as input and store it in the array string[].
2. Using for loop count the number of characters in the array string[]. Use the variable length to keep the count of the characters in the array.
3. Do step-2 till the end of input string.
4. Print the variable length as output.
Enter a string Sanfoundry The length of a string is the number of characters in it So, the length of Sanfoundry = 10
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Computer Science MCQs
- Practice BCA MCQs
- Apply for Computer Science Internship
- Buy C Books
- Watch Advanced C Programming Videos