ccplusplus.com
Learn C, C++ Concepts
Tuesday, October 4, 2011
compare two strings without using strcmp function
/*********************************************************************** * File : compare-two-string-without-strcmp.c * Author : Saurabh Gupta * Desc : Compare two strings using pointer * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : PM 11:11 04 October 2011 * Note : Try to remove the gets ************************************************************************/ #include<stdio.h> int main() { char cpFirstString[10], cpSecondString[10]; char *p, *q; int i, j, nComparisonStatus = 0; p = cpFirstString; q = cpSecondString; printf("Enter first strings : "); gets(cpFirstString); printf("Enter Second strings : "); gets(cpSecondString); for(;*p!='\0' || *q!='\0';*p++,*q++) { if(*p != *q) { printf("Strings are different\n"); nComparisonStatus=1; break; } } if(nComparisonStatus==0) { printf("Strings are same\n"); } return 0; } /* * OUTPUT * [sgupta@rhel55x86 c]$ gcc compare-two-string-without-strcmp.c -o compare-two-string-without-strcmp [sgupta@rhel55x86 c]$ ./compare-two-string-without-strcmp Enter first strings : saurabh Enter Second strings : saurabh Strings are same [sgupta@rhel55x86 c]$ ./compare-two-string-without-strcmp Enter first strings : saurabh Enter Second strings : gupta Strings are different [sgupta@rhel55x86 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment