CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2003
    Posts
    5

    Post Advanced strings comparison

    Hi guys,

    I need to find a way to compare two strings which include numbers within.
    The standard "strcmp" don't exclude numbers, so "a101" < "a12".

    Are you familiar with an implementation which sloves it?

    Regards,
    Mapisto.
    Last edited by Mapisto; October 2nd, 2005 at 01:53 PM.

  2. #2
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Advanced string comparison

    Do you want to compare numbers after ONE character? If this is the case, you can just have a pointer that points to second character's address and perform atoi/l:
    Code:
    char sourcestr1[] = "a101", sourcestr2[] = "a12";
    int a,b;
    char *p;
    p=sourcestr1 + 1;
    a=atol(p);
    b=atol(sourcestr2 + 1);
    if(a>b)...
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  3. #3
    Join Date
    Feb 2003
    Posts
    5

    Re: Advanced string comparison

    Of course not, I meant to more general solution. So, i've overcomed my laziness and eventually coded it:

    Code:
    #include <stdio.h>
    #include <ctype.h> 
    #include <string.h>
    #include <stdlib.h>
    
    
    
    int strnumcmp( const char* str1, const char* str2 )
    {
    	unsigned int str1len = strlen(str1);
    	unsigned int str2len = strlen(str2);
    	unsigned long res1 = 0, res2 =0;
    	char *stopstring1, *stopstring2;
    	for(unsigned int i =0; i < str1len; i++) {
    		if (i > str2len)
    			return 1;
    
    		if (isdigit(str1[i]) &&  isdigit(str2[i]))
    		{
    			res1 = strtoul(&str1[i], &stopstring1, 10);
    			str1 = stopstring1;
    			str1len = strlen(stopstring1);
    			res2 = strtoul(&str2[i], &stopstring2, 10);
    			str2 = stopstring2;
    			str1len = strlen(stopstring1);
    			if (res1 > res2)
    				return 1;
    			else if ( res1 < res2)
    				return -1; 
    			i = 0;
    			continue;
    		}
    
    		if (str1[i] > str2[i]) {
    			return 1;
    		} 
    		else if(str1[i] < str2[i] ) {
    			return -1;
    		}
    
    	}
    	return 0; 
    }
    
    int main( char **argv, int argc ) { 
    	const  char* str1 = "e110xy";
    	const  char* str2 = "e110xy";
    	
    	printf( "%d\n", strnumcmp(str1,str2) );
    
    	return 0;
    }
    enjoy.
    Last edited by Mapisto; October 2nd, 2005 at 03:24 PM.

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Advanced string comparison

    Code:
    int main( char **argv, int argc ) {
    Which compiler you are using?
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Advanced string comparison

    [ Moved Thread ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured