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:

  • 0 if 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 समान हैं तो 0 Return करता है।

  • यदि पहली 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 ValueDescription
0Both strings are equal
Negative valueFirst string is smaller
Positive valueFirst 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 = 0

Explanation:

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: India
Enter second string: India
Both strings are equal.

Output 2:

Enter first string: Apple
Enter second string: Banana
First string is smaller.

Output 3:

Enter first string: Mango
Enter second string: Apple
First string is greater.

Explanation of the Program

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

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

  3. Two character arrays str1 and str2 are declared to store the input strings.

  4. fgets() is used to accept both strings from the keyboard.

  5. strcspn() removes the newline character from each input string.

  6. strcmp(str1, str2) compares the two strings and stores the returned value in the variable result.

  7. If result == 0, both strings are equal.

  8. If result < 0, the first string is lexicographically smaller.

  9. Otherwise, the first string is lexicographically greater.

प्रोग्राम की व्याख्या:

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

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

  3. दो Input Strings को Store करने के लिए str1 एवं str2 Character Arrays Declare किए गए हैं।

  4. fgets() Function का उपयोग Keyboard से दोनों Strings Input करने के लिए किया गया है।

  5. strcspn() Function प्रत्येक Input String से Newline Character को हटाता है।

  6. strcmp(str1, str2) दोनों Strings की तुलना करता है तथा Return Value को Variable result में Store करता है।

  7. यदि result == 0 है तो दोनों Strings समान हैं।

  8. यदि result < 0 है तो पहली String Lexicographically छोटी है।

  9. अन्य स्थिति में पहली String Lexicographically बड़ी है।

Important Points to Remember

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

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

  3. It compares two strings character by character.

  4. It returns 0 when both strings are equal.

  5. It returns a negative or positive value when strings are different.

  6. String comparison is case-sensitive.

  7. The comparison is based on character values, not string length alone.

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

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

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

  3. यह दो Strings के Characters की तुलना करता है।

  4. दोनों Strings समान होने पर यह 0 Return करता है।

  5. Strings अलग होने पर यह Negative या Positive Value Return करता है।

  6. String Comparison Case-Sensitive होता है।

  7. String की तुलना केवल उसकी Length के आधार पर नहीं, बल्कि Characters की Values के आधार पर की जाती है।

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