+ 1
/*Comparing two strings or string comparison in C involves finding out if they are the same or not. This is done by either using some built-in function or by comparing the strings character by character to determine if they are equal. Here the second way: */ int compare_strings(char a[], char b[]) { // A variable to iterate through the strings int i = 0; while (a[i] == b[i]) { // If either of the strings reaches the end // we stop the loop if (a[i] == '\0' || b[i] == '\0') break; i++; } // We check if both the strings have been compared // till the end or not // If the strings are compared till the end they are equal if (a[i] == '\0' && b[i] == '\0') return 0; else { if(a[i] == '\0') return -1*(b[i]); else if(b[i] == '\0') return a[i]; else return (a[i]-b[i]); } }
16th Jul 2022, 8:50 PM
JaScript
JaScript - avatar
+ 3
// scanf don‘t need adress & operator for an array // you have to compare each item of the arrays or #include <stdio.h> #include <string.h> int main() { char str[10]="HASTE"; char a[10]; printf("Enter a number of five letters in capital case" ); scanf("%s",a); if(strcmp(str,a)==0) printf("YOU WON"); else printf("YOU LOSE"); return 0; }
16th Jul 2022, 12:41 PM
JaScript
JaScript - avatar