954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to see if one string is less than another string

I know you use strcmp() with strings and that if it is equal to zero, they match. But, how do you see if one string is less than the other? I want to compare SSN's to see if 12345620 is less than or greater than 123456708. Thanks for any input.

tat2dlady
Light Poster
32 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

With strcmp. The result would be less or greater than zero.

#include <stdio.h>
#include <string.h>

int main(void)
{
   static const char a[] = "12345620", b[] = "123456708";
   int result = strcmp(a, b);
   printf("%s %s %s\n", a, result < 0 ? "<" : result > 0 ? ">" : "=", b);
   return 0;
}

/* my output
12345620 < 123456708
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

>But, how do you see if one string is less than the other?
strcmp returns one of three values: 0 if the strings are of equal length and content, a value less than 0 if the first string is shorter or smaller in value than the second string, and a value greater than 0 if the first string is longer or larger in value than the second string.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

you can try using the standard library function in string.h.
use the strlen() function.here is how you do it.:
1.declare two variables say i,j for your two strings say str1,str2
2.let i=strlen(str1);j=strlen(str2);
3.if(i>j) str1 is larger string
else str2 is the larger string.
hope this is helpful.

indianscorpion2
Junior Poster in Training
82 posts since May 2005
Reputation Points: 9
Solved Threads: 1
 
#include <stdio.h>
#include <string.h>

// can't remember the header file used for isdigit - i think it is ctype, but it could be conio
#include <conio.h>
#include <ctype.h>

// macro to return the larger of the two numbers
#define IS_MAX( a, b )	( (a) > (b) ? (a) : (b) )

int main( void )
{
  static char a[] = "12345620", b[] = "123456708";
  int vA, vB, result;

  if ( !isdigit( a ) || !isdigit( b ) )	// both values needs to be numerical
    return;

  vA = atoi( a );
  vB = atoi( b );

  result = IS_MAX( vA, aB );

  printf( "%d %s %d\n", result, "<", result == vA ? vB : vA );

  return 0;
}


Ok this was written for c and not c++ but I presume it would work just the same.

Hope you come right.

Auron
Newbie Poster
11 posts since May 2005
Reputation Points: 10
Solved Threads: 1
 

>>if ( !isdigit( a ) || !isdigit( b ) )
isdigit takes a single int as its argument, and the int must be in the range of an unsigned char. Both a and b are being passed as pointers to char. Now, since the is* functions from ctype.h are typically implemented as a jump table without any bounds checking, passing a memory address would very likely cause an out of bounds access and result in an access violation crashing your program. If you're lucky of course, undefined behavior can do anything.

atoi is troublesome when it comes to error handling anyway, so you would be better off using strtol:

int safe_atoi(const char *s, int *result)
{
  char *end;

  *result = (int)strtol(s, &end, 0);

  if ((*result == 0 && end == s) || *end != '\0')
    return 0;

  return 1;
}
if (!safe_atoi(a, vA) || !safe_atoi(b, vB))
  return EXIT_FAILURE;

But that's really a lot more work than strcmp.

Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You