1
+ /*
2
+ Write a program in C to compare two strings without using string library functions.
3
+
4
+ Test Data:
5
+ Check the length of two strings:
6
+ --------------------------------
7
+ Input the 1st string: aabbcc
8
+ Input the 2nd string: abcdef
9
+ String1: aabbcc
10
+ String2: abcdef
11
+
12
+ Expected Output:
13
+ Strings are not equal.
14
+
15
+ Check the length of two strings:
16
+ --------------------------------
17
+ Input the 1st string: aabbcc
18
+ Input the 2nd string: aabbcc
19
+ String1: aabbcc
20
+ String2: aabbcc
21
+
22
+ Expected Output:
23
+ Strings are equal.
24
+ */
25
+
26
+ #include <stdio.h>
27
+
28
+ int compareStrings (char str1 [], char str2 []) {
29
+ int i = 0 ;
30
+
31
+ // Compare characters one by one
32
+ while (str1 [i ] != '\0' && str2 [i ] != '\0' ) {
33
+ if (str1 [i ] != str2 [i ]) {
34
+ return 0 ; // Strings are not equal
35
+ }
36
+ i ++ ;
37
+ }
38
+
39
+ // Check if both strings reached the end
40
+ if (str1 [i ] == '\0' && str2 [i ] == '\0' ) {
41
+ return 1 ; // Strings are equal
42
+ } else {
43
+ return 0 ; // Strings are not equal
44
+ }
45
+ }
46
+
47
+ int main () {
48
+ char str1 [100 ], str2 [100 ];
49
+
50
+ printf ("Check the length of two strings:\n" );
51
+ printf ("--------------------------------\n" );
52
+
53
+ // Input first string
54
+ printf ("Input the 1st string: " );
55
+ scanf ("%s" , str1 );
56
+
57
+ // Input second string
58
+ printf ("Input the 2nd string: " );
59
+ scanf ("%s" , str2 );
60
+
61
+ // Display the strings
62
+ printf ("String1: %s\n" , str1 );
63
+ printf ("String2: %s\n" , str2 );
64
+
65
+ // Compare strings and display result
66
+ if (compareStrings (str1 , str2 )) {
67
+ printf ("Strings are equal.\n" );
68
+ } else {
69
+ printf ("Strings are not equal.\n" );
70
+ }
71
+
72
+ return 0 ;
73
+ }
0 commit comments