Last active
August 29, 2015 14:22
-
-
Save gnitnaw/127d36d1eca96f431ef9 to your computer and use it in GitHub Desktop.
compare two strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define MAXSIZE 80 | |
void strcomp(char* p1, char* p2); | |
void read_string(char *pt); | |
int main(void) | |
{ | |
char line1[MAXSIZE]="", line2[MAXSIZE]=""; | |
printf("Enter first string : "); | |
//fgets(line1, sizeof(line1), stdin); | |
read_string(line1); | |
printf("%s\n", line1); | |
printf("Enter second string : "); | |
//fgets(line2, sizeof(line2), stdin); | |
read_string(line2); | |
printf("%s\n", line2); | |
strcomp(line1, line2); | |
return 0; | |
} | |
void read_string(char *pt) | |
{ | |
int i; | |
char c; | |
for(i=0; i < MAXSIZE-1; i++) | |
{ | |
if ((c=getchar()) != '\n' && c != EOF) | |
{ | |
pt[i] = c; | |
} else { | |
break; | |
} | |
} | |
} | |
void strcomp(char* p1, char* p2) | |
while (*p1 == *p2) { | |
if (*p1 == '\0' || *p2 == '\0') { | |
break; | |
} | |
++p1; | |
++p2; | |
} | |
if (*p1 == '\0' && *p2 == '\0') { | |
printf("They are the same! \n"); | |
} else { | |
printf("They are different! \n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment