C Program to Find the Length of a String without using the Built-in Function

This C Program finds the length of a string without using the built-in function. This program is used to find the length of the string means total number of characters present in a given string. This program need to be done without using the built-in-function. The built in functions are those which have built- in already which can be used directly.

Here is source code of the C program to finds 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.

  1. /*
  2.  * C program to find the length of a string without using the
  3.  * built-in function
  4.  */
  5. #include <stdio.h>
  7. void main()
  8. {
  9.     char string[50];
  10.     int i, length = 0;
  12.     printf("Enter a string \n");
  13.     gets(string);
  14.     /*  keep going through each character of the string till its end */
  15.     for (i = 0; string[i] != '\0'; i++)
  16.     {
  17.         length++;
  18.     }
  19.     printf("The length of a string is the number of characters in it \n");
  20.     printf("So, the length of %s = %d\n", string, length);
  21. }

$ cc pgm52.c
$ a.out
Enter a string
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 Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.
Subscribe Newsletter & Posts

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

Practice Interview Questions & Answers, Quizzes, Objective, Multiple Choice Aptitude Tests for Freshers and Experienced People in these topics - Learn C, C++, Java and C# Programming with coding example on Simple problems as well as tough Algorithms and Data-structures along with runtime output: - C Programs, Data-Structures and Algorithms, C++ Algorithms, Java Algorithms, C# Programs, Android Programs in Java

C Tutorials and Linux Commands Tutorial - C Tutorials, Linux Commands

Career Mentoring with our Founder / CTO - Read More