Skip to content

Instantly share code, notes, and snippets.

@gnitnaw
Last active August 29, 2015 14:22
Show Gist options
  • Save gnitnaw/127d36d1eca96f431ef9 to your computer and use it in GitHub Desktop.
Save gnitnaw/127d36d1eca96f431ef9 to your computer and use it in GitHub Desktop.
compare two strings
#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