C Program to Compare Two Strings Using strcmp() (strcmp() फंक्शन द्वारा दो स्ट्रिंग्स की तुलना करने का C प्रोग्राम)
What is strcmp() Function in C?
In C language, the strcmp() function is used to compare two strings character by character. It compares the strings lexicographically (according to character values).
The strcmp() function returns:
0if both strings are equal.A negative value if the first string is smaller than the second string.
A positive value if the first string is greater than the second string.
The strcmp() function is declared in the standard header file string.h.
C भाषा में strcmp() फंक्शन क्या है?
C लैंग्वेज में strcmp() फंक्शन का प्रयोग दो Strings की तुलना (Compare) करने के लिए किया जाता है। यह दोनों Strings के Characters की तुलना उनके Character Values के आधार पर करता है।
strcmp() फंक्शन निम्नलिखित परिणाम Return करता है:
यदि दोनों Strings समान हैं तो
0Return करता है।यदि पहली String दूसरी String से छोटी है तो Negative Value Return करता है।
यदि पहली String दूसरी String से बड़ी है तो Positive Value Return करता है।
strcmp() फंक्शन को C भाषा की Standard Header File string.h में Declare किया गया है।
Syntax of strcmp() Function
strcmp(string1, string2);Function Prototype:
int strcmp(const char *string1, const char *string2);Return Values:
| Return Value | Description |
|---|---|
0 | Both strings are equal |
| Negative value | First string is smaller |
| Positive value | First string is greater |
व्याख्या:
| Return Value | विवरण |
|---|---|
0 | दोनों Strings समान हैं |
| Negative Value | पहली String छोटी है |
| Positive Value | पहली String बड़ी है |
Example of strcmp() Function
char str1[] = "Apple";char str2[] = "Apple";int result;result = strcmp(str1, str2);
Output:
result = 0Explanation:
In the above example, both strings contain the same characters. Therefore, the strcmp() function returns 0, indicating that both strings are equal.
व्याख्या:
उपरोक्त उदाहरण में str1 एवं str2 दोनों में समान Characters हैं। इसलिए strcmp() फंक्शन 0 Return करता है, जो यह दर्शाता है कि दोनों Strings समान हैं।
C Program to Compare Two Strings Using strcmp()
Program:
#include <stdio.h>#include <string.h>int main(){char str1[255], str2[255];int result;printf("Enter first string: ");fgets(str1, sizeof(str1), stdin);str1[strcspn(str1, "\n")] = '\0';printf("Enter second string: ");fgets(str2, sizeof(str2), stdin);str2[strcspn(str2, "\n")] = '\0';result = strcmp(str1, str2);if(result == 0){printf("Both strings are equal.");}else if(result < 0){printf("First string is smaller.");}else{printf("First string is greater.");}return 0;}
Output 1:
Enter first string: IndiaEnter second string: IndiaBoth strings are equal.
Output 2:
Enter first string: AppleEnter second string: BananaFirst string is smaller.
Output 3:
Enter first string: MangoEnter second string: AppleFirst string is greater.
Explanation of the Program
stdio.his used for input and output operations.string.his included to use thestrcmp()andstrcspn()functions.Two character arrays
str1andstr2are declared to store the input strings.fgets()is used to accept both strings from the keyboard.strcspn()removes the newline character from each input string.strcmp(str1, str2)compares the two strings and stores the returned value in the variableresult.If
result == 0, both strings are equal.If
result < 0, the first string is lexicographically smaller.Otherwise, the first string is lexicographically greater.
प्रोग्राम की व्याख्या:
stdio.hका उपयोग Input एवं Output Operations के लिए किया जाता है।string.hहेडर फाइल कोstrcmp()एवंstrcspn()फंक्शन्स के उपयोग के लिए शामिल किया गया है।दो Input Strings को Store करने के लिए
str1एवंstr2Character Arrays Declare किए गए हैं।fgets()Function का उपयोग Keyboard से दोनों Strings Input करने के लिए किया गया है।strcspn()Function प्रत्येक Input String से Newline Character को हटाता है।strcmp(str1, str2)दोनों Strings की तुलना करता है तथा Return Value को Variableresultमें Store करता है।यदि
result == 0है तो दोनों Strings समान हैं।यदि
result < 0है तो पहली String Lexicographically छोटी है।अन्य स्थिति में पहली String Lexicographically बड़ी है।
Important Points to Remember
strcmp()is a standard string handling function in C.It is declared in the
string.hheader file.It compares two strings character by character.
It returns
0when both strings are equal.It returns a negative or positive value when strings are different.
String comparison is case-sensitive.
The comparison is based on character values, not string length alone.
महत्वपूर्ण बिंदु:
strcmp()C भाषा का एक Standard String Handling Function है।यह
string.hHeader File में Declare किया गया है।यह दो Strings के Characters की तुलना करता है।
दोनों Strings समान होने पर यह
0Return करता है।Strings अलग होने पर यह Negative या Positive Value Return करता है।
String Comparison Case-Sensitive होता है।
String की तुलना केवल उसकी Length के आधार पर नहीं, बल्कि Characters की Values के आधार पर की जाती है।
Comments
Post a Comment