请问一个字符串"11,2222"如何拆成两个整型11和2222,其中“11,2222”的长度不确定

8213664 2003-02-13 09:27:36
请问一个字符串"11,2222"如何拆成两个整型11和2222,其中“11,2222”的长度不确定,逗号两边的串由输入而定。
...全文
119 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuren_zhang 2003-02-14
  • 打赏
  • 举报
回复
strtok
flyer_2001 2003-02-14
  • 打赏
  • 举报
回复
可用循环
用楼上的方法
bm1408 2003-02-14
  • 打赏
  • 举报
回复
agree up !
allen1981813 2003-02-14
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
void main(){
char str[]="11,2222";
char *buf;
buf=strtok(str,",");
char s1[225];
strcpy(s1,buf);
buf=strtok(NULL,"\0");
char s2[225];
strcpy(s2,buf);
cout<<"s1="<<s1<<endl
<<"s2="<<s2<<endl;
}
normalnotebook 2003-02-14
  • 打赏
  • 举报
回复
#include <string.h>
#include <stdio.h>
void main( void )
{
char string[100],*token;
gets(string);
token = strtok( string, ',' );
while( token != NULL )
{
printf( " %ld\n", atol(token) );
token = strtok( NULL, ',' );
}
}
//没有调试,大概就是这样吧
就是先用strtok()函数分割,再用atol()函数转换,最好不用atoi(),因长度不固定,也不知道大小,以免溢出
Kevin__Li(凯文) 的方法不是很好,因为,你不知道输入多少数据,如果是11,2222,3333,则他的程序无法处理了,按照他的想法,则需要大量的变量
,也就是程序不够健壮
softman_2000 2003-02-14
  • 打赏
  • 举报
回复
还是 Kevin__Li(凯文) 的做法跟我一样。
idler 2003-02-14
  • 打赏
  • 举报
回复
用strtok。
Kevin__Li 2003-02-14
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
char str[] = "1234,5678";
int a, b;

sscanf(str, "%d,%d", &a, &b);

printf("str =%s\n", str);
printf("a =%d\tb =%d\n", a, b);

return 0;
}
sailor_Song 2003-02-13
  • 打赏
  • 举报
回复
lcftc(lcftc)您的程序中是如下:
str1=str.substr(0,x);
str2=str.substr(x+1,str.size());
是不是应该:
str1=str.substr(0,x-1);
str2=str.substr(x+1,str.size());
8213664 2003-02-13
  • 打赏
  • 举报
回复
请帮忙给个C的实现
windcsn 2003-02-13
  • 打赏
  • 举报
回复
同样,你只要将string改为char*
然后逐个字符串查找,稍做修改,但基本思想不变
8213664 2003-02-13
  • 打赏
  • 举报
回复
我的意思是在C语言里实现
cjnet 2003-02-13
  • 打赏
  • 举报
回复
先分隔,再转换。
Delphist 2003-02-13
  • 打赏
  • 举报
回复
char* a=strtok(str,',');
char* b=strtok(NULL,',');
int i=atoi(a);
int j=atoi(b);
kp_flysky 2003-02-13
  • 打赏
  • 举报
回复
对,找到分隔符!分隔成两个字符串,在转换就可以了
lcftc 2003-02-13
  • 打赏
  • 举报
回复
windcsn(各位新年洋洋得意) 说得对,下面是将他的代码细化,VC6,Dev-C++编译通过;
#include<iostream>
#include<string>
using namespace std;
void main()
{
string str,str1,str2;
cin>>str;
int x=str.find(',');//找到","是第几个
str1=str.substr(0,x);
str2=str.substr(x+1,str.size());

int number1,number2;

number1=atoi(str1.c_str());//atoi(const char *)是将字符串变为int
number2=atoi(str2.c_str());

cout<<number1<<","<<number2<<endl;

}
windcsn 2003-02-13
  • 打赏
  • 举报
回复
void spilttwo(string src,int &a,int &b)
{
int pos = src.find(",");
string temp = src.substr(0,pos);
a = atoi(temp.c_str());
temp = src.substr(pos,src.size());
b = atoi(temp.c_str());
}
上面的没有调试,没有判断字符串是否为空
windcsn 2003-02-13
  • 打赏
  • 举报
回复
先根据,号拆分成两个字符串,然后将字符串转换成数字即可(用atoi,atol,atof等)
xzhuang 2003-02-13
  • 打赏
  • 举报
回复
char* ch = strchr(str, ',');
*ch = 0;
int i = atol(str);
int j = atol(ch + 1);

qdcb 2003-02-13
  • 打赏
  • 举报
回复
string 类中好像有split 的成员吧、

70,031

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧