How to compare strings ignoring case

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dan5348
    New Member
    • Oct 2014
    • 5

    #1

    How to compare strings ignoring case

    Im lost on this.

    It should report whether or not, ignoring case, they are the same.

    Code:
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    void main(void)
    {
    	char str1[25], str2[25], dif;
    	printf("\nEnter the first String:");
    	gets(str1);
    	printf("\nEnter the second String;");
    	gets(str2);
    
    	dif = strcmp(str1, str2);                                       //using string functions
    
    	if (dif>0)
    		printf("%s comes after %s", str1, str2);
    	else
    	{
    		if (dif<0)
    			printf("%s comes after %s", str2, str1);
    		else
    			printf("both the strings are same");
    	}
    }
    Last edited by Frinavale; Oct 30 '14, 07:43 PM.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    strcmp just compares the strings. Therefore, X and x will compare not equal.

    Use stricmp. That is a case insensitive version of strcmp.

    Comment

    Working...