How to Find Length of a String in C

How to Find Length of a String in C Language with Examples

In this article, I am going to discuss How to Find the Length of a String in C Language with Examples. Please read our previous article where we discussed Predefined String Functions in C Language with Examples.

How to Find Length of a String in C Language?

In this article, we will be writing functions upon the string. So commonly used functions we will write and most of these functions are available as library functions. So, we will learn how these functions work so that if required we can develop our own logic for performing any operation which is not available. So let us start with the first operation that is finding the length of our string.

How to Find Length of a String in C Language with Examples

We want to find out the length of the above string means the number of characters present in a string. So, we have to count all the characters until we reach ‘\0’. Here, the length of the string is 5 means there are 5 characters in the array.

There are 5 alphabets, as the indices are starting from 0 onwards. So, at 5 we have ‘\0’. We know that the size of the array is 6 but the size of the string is 5 as we are not counting null character (‘\0’). The size of an array can be anything that size can be bigger than the size of the string. With the help of ‘\0’, we can find the length of the string.

So, the procedure is very simple we just have to scan this array of characters until we reach ‘\0’.

How to Find Length of a String in C Language

So, for that, we can take our individual pointer j and we can go on looking for ‘\0’. So below is the full program for finding the length of a string in C Language

Find Length of a String Code in C language:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
    char *S = "hello";
    int j;

    for (j = 0; S[j] != '
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char *S = "hello";
int j;
for (j = 0; S[j] != '\0'; j++)
{
// no code here 
}
printf ("String is \"%s\"\n", S);
printf ("Length is %d", j);
return 0;
}
'; j++) { // no code here } printf ("String is \"%s\"\n", S); printf ("Length is %d", j); return 0; }
Output:

Find Length of a String Code in C language

How to Find Length of a String Using Built-in strlen() String Function?

By using this strlen() predefined function, we can find the length of a given string in C Language. The strlen() function requires 1 arguments of type (const char*) and returns an int type. When we are working with strlen() from the given address up to \0, the entire character count value will return.

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main() 
{   
    char str[] = "hello";
    int s,l;
    s=sizeof(str);
    l=strlen(str);
    printf("\nsize: %d",s);
    printf("\nlength: %d",l);
    getch();
    return 0; 
}
Output:

How to Find Length of a String Using Built-in strlen() String Function?

In the next article, I am going to discuss How to Change the Case of the Alphabet in a String in C Language with Examples. Here, in this article, I try to explain How to Find the Length of a String in C Language with Examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *