注册 登录
编程论坛 C++教室

实现如下功能 该怎么写呢?

kldyutou 发布于 2010-01-10 17:30, 462 次点击
,按要求实现下面功能
a)定义字符串 “123&456&789”和一个长度为3的整形数组
b)把字符串中的以'&'分割开的数字解析出来并且存储在数组中。
c)函数声明如下:
   void split (const char *source,int *iarray);

[ 本帖最后由 kldyutou 于 2010-1-10 17:33 编辑 ]
1 回复
#2
kspliusa2010-01-11 10:05
回复 楼主 kldyutou
#include <iostream>
#include <ctype.h>
#include <string>

using namespace std;

void split (const char *source,int *iarray){
    int str_len = strlen (source);
    int i, j=0;
    for (i=0; i<str_len; i++){
        if ((*(source+i) != '&') && isdigit(*(source+i))){
            *(iarray+j) = *(source+i)-'0';
            j++;
        }
    }
    for (i=0; i<j; i++)
        cout << *(iarray+i)<<endl;
}

在vc++6.0上编译的
1