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.stringis the string whose length is to be calculated.size_tis 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 = 5Explanation:
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:IndoreLength of string Indore = 6
Explanation:
stdio.his used for input and output operations.string.his included to use thestrlen()andstrcspn()functions.An integer variable
iis declared to store the length of the string.A character array
s[255]is declared to store the input string.fgets()is used to accept a string from the keyboard, including spaces.strcspn()is used to locate the newline character ('\n') and replace it with the null character ('\0').strlen(s)calculates the length of the string and stores the result in variablei.Finally,
printf()displays the string and its length.
व्याख्या:
stdio.hहेडर फाइल का उपयोग Input एवं Output Operations के लिए किया जाता है।string.hहेडर फाइल कोstrlen()एवंstrcspn()फंक्शन्स के उपयोग के लिए शामिल किया गया है।String की Length Store करने के लिए Integer Variable
iDeclare किया गया है।Input String को Store करने के लिए Character Array
s[255]Declare किया गया है।fgets()Function का उपयोग Keyboard से String Input करने के लिए किया गया है। यह Spaces सहित String Input कर सकता है।strcspn()Function की सहायता से Newline Character ('\n') को खोजकर उसके स्थान पर Null Character ('\0') रखा गया है।strlen(s)Function String की Length ज्ञात करके उसका परिणाम Variableiमें Store करता है।अंत में
printf()Function String तथा उसकी Length Display करता है।
Important Points to Remember
strlen()is a standard string handling function in C.It is declared in the
string.hheader file.It returns the length of a string excluding the null character.
Its return type is
size_t, notint.strlen("India")returns 5.strlen()does not modify the original string.
महत्वपूर्ण बिंदु:
strlen()C भाषा का एक Standard String Handling Function है।यह
string.hHeader File में Declare किया गया है।यह Null Character को छोड़कर String की Length Return करता है।
इसका Return Type
size_tहोता है,intनहीं।strlen("India")का परिणाम 5 होता है।strlen()Original String में कोई परिवर्तन नहीं करता है।
Comments
Post a Comment