---
title: "Compare two strings with function"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:string-compare-function"
source: https://www.bctengineer.com/q/semester_1/computer_programming/string-compare-function
site: BCT Engineering
---

# Compare two strings with function

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2073 Shrawan, 4 marks
- Video: [User function in C to compare given strings](https://www.youtube.com/watch?v=_X1jnJHomWA) — WIT Solapur - Professional Learning Community
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/string-compare-function
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a C program to read two strings in main and compare them using userdefined function. Display appropriate message from main.

## Short answer

The compare() function iterates while characters match and string not ended, returns difference of current characters (0 if equal). main() reads strings, calls compare, and prints 'equal' if returned 0 else 'not equal'.

## Answer

**Logic.** The function `compare()` moves along both strings while the characters are equal and the first string has not ended. When it stops, the difference between the two current characters is returned: 0 means equal, a positive value means the first string is greater, and a negative value means smaller (like `strcmp`). `main()` reads the strings and displays the message.

```c
#include <stdio.h>

/* returns 0 if equal, positive if a > b, negative if a < b */
int compare(char a[], char b[])
{
    int i = 0;
    while (a[i] != '\0' && a[i] == b[i])
        i++;
    return a[i] - b[i];
}

int main(void)
{
    char s1[50], s2[50];
    printf("Enter first string: ");
    scanf("%49s", s1);
    printf("Enter second string: ");
    scanf("%49s", s2);
    int r = compare(s1, s2);
    if (r == 0)
        printf("Strings are equal\n");
    else
        printf("Strings are not equal\n");
    return 0;
}
```

**Sample run**

Input: `nepal nepali`

```text
Enter first string: Enter second string: Strings are not equal
```

"nepal" and "nepali" differ at the end (`'\0'` against `'i'`), so the strings are not equal.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When does the compare loop stop?
   A. After exactly one comparison
   B. When the first string ends or the characters differ
   C. Only when both strings end
   D. After strlen of the second string
   Answer: B. It runs while a[i] != '\0' and a[i] == b[i]. (Key point: Loop condition)

2. What does compare return for equal strings?
   A. 1
   B. -1
   C. The length
   D. 0
   Answer: D. The difference of '\0' and '\0' is 0. (Key point: Return value)

3. Why are "nepal" and "nepali" not equal?
   A. Their first letters differ
   B. '\0' differs from 'i' at the end
   C. They have the same length
   D. Case differs
   Answer: B. The loop stops at index 5 where the characters differ. (Key point: Different lengths)

## Related in Computer Programming

- [Significance of main() function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-of-main) — Computer Programming, chapter 6
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6
- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
- [Sort array using a function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-sort-function) — Computer Programming, chapter 7

## Related in other subjects

- [Join two strings with dynamic constructor](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Ajoin-strings-dynamic-constructor) — Object Oriented Programming, chapter 4
- [Compare TSI write/read modes](https://www.bctengineer.com/question.md?key=semester_8%2Ftelecommunications%3Atsi-write-read-modes-compare) — Telecommunications, chapter 4
- [Steam main with two insulation layers: overall coefficients](https://www.bctengineer.com/question.md?key=semester_2%2Ffundamental_of_thermodynamics_and_heat_transfer%3Asteam-main-two-layer-lagging-overall-u) — Fundamental of Thermodynamics & Heat Transfer, chapter 7
- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
