- Replace all non-digit characters with
' '
. - Trim spaces at the start.
- Trim spaces between numbers, leave only one.
- Put
'\0'
after last digit. - Replace the one space with
':'
.
If the numbers may be inversed in order, you may use this (code this time, as you wish):
#include <string.h>
#include <stdio.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
int main()
{
char src[] = ".>1220and.<300";
char dstBuf[64]="ERROR";
char* num1 = NULL;
char* num2 = NULL;
char* ptr = src;
int indigit = 0;
for (;*ptr; ptr++) {
if (isdigit(*ptr)) {
if (indigit == 0) {
if (num1 == NULL) num1 = ptr;
else num2 = ptr;
indigit = 1;
}
} else {
indigit = 0;
}
}
if (num1 != NULL && num2 != NULL) {
int n1 = strtoll(num1, NULL, 10);
int n2 = strtoll(num2, NULL, 10);
sprintf(dstBuf, "%d:%d", MIN(n1,n2), MAX(n1,n2));
} else if (num1 != NULL) {
sprintf(dstBuf, "%s", num1);
}
printf("%s\n", dstBuf);
}