Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
3.
Example
4.
Solution
4.1.
Code in C
5.
Frequently Asked Questions
5.1.
Why is C called a mid-level programming language?
5.2.
What is a built-in function in C?
5.3.
What are header files, and what are their uses in C?
5.4.
How are strings stored in C?
5.5.
How is a string stored in memory?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C Program to count the length of string without using any library function

Author Komal Shaw
0 upvote

Introduction

Finding out the length of the string is an essential step in programming. There are library functions to find out the length of the string. But in this article, we will learn how to find out the length without using any library function.

Also Read, Binary to Hex Converter,C Static Function

Problem Statement

Write a code in C to find out the length of the string without using any library function.

Example

 

The length of the given string = “CODINGNINJAS”  is 12.

You can also read about dynamic array in c and  Tribonacci Series.

Solution

We will run a for loop until the end of the string and keep a count of the length of the string, i.e.; we will iterate over the characters of the string from i = 0 to until ‘\0’ is encountered. We will keep increasing the count of i by 1 after each iteration.

Thus when the loop ends, we will get the length of the string stored in i.

Code in C

#include <stdio.h>
int main() {
   //code to find out the length of the given string without
   //using any library function.
   char s[] = "CODINGNINJAS";
   int i;

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

 

Output

Length of the string: 12


You can practice by yourself with the help of online c compiler.

Frequently Asked Questions

Why is C called a mid-level programming language?

C is a mid-level programming language because it bridges low-level and high-level computer languages. We may utilize the C language as a System programming language to create the operating system and an Application programming language to create a menu-driven customer-driven billing system. 

What is a built-in function in C?

Built functions, also known as the library functions, are supplied by the system to make a developer's life easier by supporting them in some commonly used predetermined activities. In C, for example, if we need to send the output of our program to the terminal, we would use printf().

scanf(), printf(), strcpy, strcmp, strlen, strcat, and many more built-in functions are the most widely used in C.

What are header files, and what are their uses in C?

In C, header files have the extension .h, these header files include function definitions, data type definitions, macros, and so on. The header is used to import the above definitions into the source code, this is done by using #include before the header.  For example, if your source code requires taking input from the user, doing some modification, and printing the output on the terminal, it should include the stdio.h file as #include <stdio.h>, which allows the user to take input using scanf(), and to perform some manipulations, and after that print using printf().

How are strings stored in C?

C stores strings of characters as arrays of chars, terminated by a null byte.

How is a string stored in memory?

A string is stored in memory using consecutive memory cells and the string is terminated by the sentinel '\0'.

Conclusion

In this article, we have extensively discussed the Booting in an operating system. We hope this blog has helped you enhance your knowledge regarding Booting in an operating system.

If you want to learn more, check out our articles on Binary to decimal in CArmstrong Number in CRandom function in C, Short int in C Programming 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 
Check out this problem - Longest String Chain

Enrol in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

 

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass