C Program to Find the Length of a String Using strlen() (strlen() फंक्शन द्वारा स्ट्रिंग की लंबाई ज्ञात करने का C प्रोग्राम)

What is strlen() Function in C?

In C language, the strlen() function is used to calculate the length of a string. It returns the total number of characters present in a string, excluding the null character ('\0'). The strlen() function is declared in the standard header file string.h.

C भाषा में strlen() फंक्शन क्या है?

C लैंग्वेज में strlen() फंक्शन का प्रयोग String की Length (लंबाई) ज्ञात करने के लिए किया जाता है। यह फंक्शन String में उपस्थित कुल Characters की संख्या Return करता है, लेकिन Null Character ('\0') को Count नहीं करता है। strlen() फंक्शन को C भाषा की Standard Header File string.h में Declare किया गया है।

Syntax of strlen() Function

strlen(string);

Function Prototype:

size_t strlen(const char *string);

Here:

  • strlen() is the name of the function.

  • string is the string whose length is to be calculated.

  • size_t is an unsigned integer type used to represent the size or length of an object.

व्याख्या:

  • strlen() फंक्शन का नाम है।

  • string उस String को दर्शाता है जिसकी Length ज्ञात करनी है।

  • size_t एक Unsigned Integer Type है, जिसका उपयोग किसी Object का Size या Length दर्शाने के लिए किया जाता है।

Example of strlen() Function

int i;
char s[10] = "india";
i = strlen(s);

Output:

i = 5

Explanation:

In the above example, the string "india" contains five characters. Therefore, the strlen() function returns 5, which is assigned to the variable i.

व्याख्या:

उपरोक्त उदाहरण में "india" String में कुल पाँच Characters हैं। इसलिए strlen() फंक्शन 5 Return करता है, जिसे Variable i में Store किया जाता है।

C Program to Find the Length of a String Using strlen()

Program:

#include <stdio.h>
#include <string.h>
int main()
{
int i;
char s[255];
printf("Enter a string: ");
fgets(s, sizeof(s), stdin);
s[strcspn(s, "\n")] = '\0';
i = (int)strlen(s);
printf("Length of string %s = %d\n", s, i);
return 0;
}

Output:

Enter a string:
Indore
Length of string Indore = 6

Explanation:

  1. stdio.h is used for input and output operations.

  2. string.h is included to use the strlen() and strcspn() functions.

  3. An integer variable i is declared to store the length of the string.

  4. A character array s[255] is declared to store the input string.

  5. fgets() is used to accept a string from the keyboard, including spaces.

  6. strcspn() is used to locate the newline character ('\n') and replace it with the null character ('\0').

  7. strlen(s) calculates the length of the string and stores the result in variable i.

  8. Finally, printf() displays the string and its length.

व्याख्या:

  1. stdio.h हेडर फाइल का उपयोग Input एवं Output Operations के लिए किया जाता है।

  2. string.h हेडर फाइल को strlen() एवं strcspn() फंक्शन्स के उपयोग के लिए शामिल किया गया है।

  3. String की Length Store करने के लिए Integer Variable i Declare किया गया है।

  4. Input String को Store करने के लिए Character Array s[255] Declare किया गया है।

  5. fgets() Function का उपयोग Keyboard से String Input करने के लिए किया गया है। यह Spaces सहित String Input कर सकता है।

  6. strcspn() Function की सहायता से Newline Character ('\n') को खोजकर उसके स्थान पर Null Character ('\0') रखा गया है।

  7. strlen(s) Function String की Length ज्ञात करके उसका परिणाम Variable i में Store करता है।

  8. अंत में printf() Function String तथा उसकी Length Display करता है।

Important Points to Remember

  1. strlen() is a standard string handling function in C.

  2. It is declared in the string.h header file.

  3. It returns the length of a string excluding the null character.

  4. Its return type is size_t, not int.

  5. strlen("India") returns 5.

  6. strlen() does not modify the original string.

महत्वपूर्ण बिंदु:

  1. strlen() C भाषा का एक Standard String Handling Function है।

  2. यह string.h Header File में Declare किया गया है।

  3. यह Null Character को छोड़कर String की Length Return करता है।

  4. इसका Return Type size_t होता है, int नहीं।

  5. strlen("India") का परिणाम 5 होता है।

  6. strlen() Original String में कोई परिवर्तन नहीं करता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025