How to write a C Program to Compare Two Character in C Programming Language ?
Solution:
- #include <stdio.h>
- #include <string.h>
- int compare(char x1[], char x2[])
- {
- int n1, n2, ok, i, j, n;
- n1 = strlen(x1);
- n2 = strlen(x2);
- ok = 0;
- if(n1>n2)
- {
- n = n2;
- }
- else
- if(n1<n2)
- {
- n = n1;
- }
- else
- {
- n = n1;
- }
- for(i=0;i<n;i++)
- {
- if(x1[i]>x2[i])
- {
- ok = 1;
- return ok;
- }
- if(x1[i]<x2[i])
- {
- ok = -1;
- return ok;
- }
- }
- if(n1 > n2)
- {
- ok = 1;
- return ok;
- }
- if(n2 > n1)
- {
- ok = -1;
- return ok;
- }
- ok = 0;
- return ok;
- }
- int main()
- {
- char x1[1000], x2[1000], c;
- int n1, n2, ok;
- scanf("%s", x1);
- scanf("%c", &c);
- scanf("%s", x2);
- ok = compare(x1, x2);
- printf("%d\n", ok);
- return 0;
- }