ccplusplus.com
Learn C, C++ Concepts
Sunday, September 11, 2011
compare two string using pointers
/****************************************************** * File : compare-two-string-using-pointers.c * Author : Saurabh Gupta * Desc : To compare two strings using pointer * Source : http://saurabhgupta0527.blogspot.com/ * Created : AM 10:11 11 September 2011 * Note : *****************************************************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <conio.h> int main(int argc, char **argv) { char strFirstString[10], strSecondString[10]; char *p, *q; int i, j, nStrCompStatus = 0; p = strFirstString; q = strSecondString; printf("\nInput two strings\n"); gets (strFirstString); // get the first string gets (strSecondString); // get the second string for(; *p != '\0' || *q!='\0'; *p++,*q++) { if(*p!=*q) { printf("\nStrings are different\n"); nStrCompStatus=1; break; } } if(nStrCompStatus==0) { printf("\nStrings are same\n"); } return 0; } /* * OUTOUT * [sgupta@rhel54x64 c]$ [sgupta@rhel54x64 c]$ gcc compare-two-string-using-pointers.c -o compare-two-string-using-pointers [sgupta@rhel54x64 c]$ ./compare-two-string-using-pointers Input two strings saurabh saurav Strings are different [sgupta@rhel54x64 c]$ ./compare-two-string-using-pointers Input two strings saurabh saurabh Strings are same [sgupta@rhel54x64 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