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.
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;
}
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.