LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 761|回复: 8

字符串问题

[复制链接]
发表于 2005-8-30 18:16:38 | 显示全部楼层 |阅读模式
我有3串字符串,型如:1222AAAAAAA,1333SSSSSSSS,1223DDDDDDDDDD.现在我要将这3字符串的非数字部分按照数字部分的大小排序,各位大虾有何办法,教教我啊
发表于 2005-8-30 18:49:29 | 显示全部楼层
这个的关键是把字符的数字取出来...
我就写了这部分....




/*
* Complie the code under UNIX system,please.
* Author : missanda@hotmail.com   8907673
*
* $Header$
*
**/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>

const char *str[] = {
        "1222AAAAAAA",
        "1333SSSSSSSS",
        "1223DDDDDDDDDD",
};


int fetch_number(const char*s)
{

        int i = 0;
        char tmp[sizeof(int)*8];

        if(!s){
                fprintf(stderr,"%s failed - !s\n",__FUNCTION__);
                return -EINVAL;
        }

        if(strlen(s)>sizeof(tmp)) {
                fprintf(stderr,"%s failed - string is too long.\n",__FUNCTION__);
                return -EINVAL;
        }

        bzero(tmp,sizeof(tmp));

        for(i = 0;i<strlen(s);i++){

                if( s <= '9' || s >= '0' ){
                        //is a number. ASCII code '0' -->>  '9'
                        tmp = s;
                        continue;
                }
                break;

        }

        if( tmp[0] == 0x00 ){
                fprintf(stderr,"%s failed - no one number found in string - \"%s\"\n",__FUNCTION__,s);
                return -EINVAL;
        }

        return atoi(tmp);
}






int main(int argc,char**argv)
{

        char *res[sizeof(str)];
        int i = 0;
        int min = 0;


#if 0
        //debug  fetch_number
        printf("%s == >> %d\n",str[0],fetch_number(str[0]));
#endif

        return 0;
}
回复 支持 反对

使用道具 举报

发表于 2005-8-30 19:48:42 | 显示全部楼层
如果是数字都是开头几个
可以试一下标准C函数库里的strtol函数
他能将字串前几个字符转成整数

详细man strtol
回复 支持 反对

使用道具 举报

发表于 2005-8-30 19:55:02 | 显示全部楼层
补充一下:
如果前面的数字位数相同,甚至可以直接比较:
如str1,str2指向两个待比较的字串:
*str > * str直接比较大小
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-9-2 10:03:22 | 显示全部楼层
现在你们说的是提取数字部分,我想要的是         安数字部分排后面的字符串
回复 支持 反对

使用道具 举报

发表于 2005-9-2 10:08:28 | 显示全部楼层
数字部分都出来了,排序还不是水到渠成
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-9-2 14:36:31 | 显示全部楼层
大大,我不会才要问哦,水到渠成的问题我不会啊,说个方法撒
回复 支持 反对

使用道具 举报

发表于 2005-9-2 15:55:28 | 显示全部楼层
最简单的插入排序就行了
回复 支持 反对

使用道具 举报

发表于 2005-9-3 17:07:00 | 显示全部楼层
The ISO C function qsort is an alternative.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表