This C Program accepts two strings & compares them. The program accepts two strings say string1 and string2. If both the strings are equal then it display both strings are equal. If string1 > string2 then display a appropriate message and so on.
Here is source code of the C program to accepts two strings & compare them. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C Program to accepts two strings and compare them. Display
* the result whether both are equal, or first string is greater
* than the second or the first string is less than the second string
int count1 = 0, count2 = 0, flag = 0, i;
char string1, string2;
printf("Enter a string:");
printf("Enter another string:");
/* Count the number of characters in string1 */
while (string1[count1] != '\0')
/* Count the number of characters in string2 */
while (string2[count2] != '\0')
i = 0;
while ((i < count1) && (i < count2))
if (string1[i] == string2[i])
if (string1[i] < string2[i])
flag = -1;
if (string1[i] > string2[i])
flag = 1;
if (flag == 0)
printf("Both strings are equal \n");
if (flag == 1)
printf("String1 is greater than string2 \n", string1, string2);
if (flag == -1)
printf("String1 is less than string2 \n", string1, string2);
$ cc pgm50.c /$ a.out Enter a string: hello Enter another string: world String1 is less than string2 $ a.out Enter a string:object Enter another string:class String1 is greater than string2 $ a.out Enter a string:object Enter another string:object Both strings are equal
Sanfoundry Global Education & Learning Series – 1000 C Programs.
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.