请教一字符串处理函数

yaningou 2003-10-17 10:59:55
假设有vc_str1的值为"1=12345;2=123",我要将"1="与";"之间的"12345"赋于vc_str2,
将"2="后的"123"赋于vc_str3.
当vc_str_str1的值为"1=12345"时得到vc_str2="12345",vc_str3=""
当vc_str_str2的值为""时,vc_str2="",vc_str3=""

请问该函数如何写?

...全文
33 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaningou 2003-10-17
  • 打赏
  • 举报
回复
假设有vc_str1的值为"1=12345;2=123",我要将"1="与";"之间的"12345"赋于vc_str2,
将"2="后的"123"赋于vc_str3.
当vc_str_str1的值为"1=1234阿5"时得到vc_str2="12345",vc_str3=""
当vc_str_str1的值为""时,vc_str2="",vc_str3=""

请问该函数如何写?(其中vc_str1,vc_str2,vc_str3都为char型)
njuhuangmy 2003-10-17
  • 打赏
  • 举报
回复
这个 只是 方法

还要 注意 判断, str1 不要出界
njuhuangmy 2003-10-17
  • 打赏
  • 举报
回复
当vc_str_str2的值为""时,vc_str2="",vc_str3=""
1??

设字符串用的是 指针
while (*str1 != '=')
str1++;
str1++;

len = 0;
while (*str1 != ';')
str2[len++] = *str1++;

在外围再循环,可以得 str2, 当然,还要一个 整型的数 来控制 str3....
ghxmagic 2003-10-17
  • 打赏
  • 举报
回复
#include <stdio.h>
void main(void)
{
char *vc_str1="1=12345;2=123";
char *vc_str2;
char *vc_str3;
bool bflag,bflags;
bflag=false;
bflags=false;
vc_str2=vc_str1;
vc_str3=vc_str1;
while(*vc_str1!='\0')
{
if((*vc_str1=='1')&&(*(vc_str1+1)=='='))
{
bflag=true;
vc_str1++;
vc_str2=vc_str1++;
continue;
}
if((*vc_str1==';')&&(*(vc_str1+1)=='2')&&(*(vc_str1+2)=='='))
{
bflags =true;
bflag=false;
vc_str1+=2;
vc_str3=vc_str1;
continue;
}
if(bflag==true)
{
vc_str2++;

vc_str1++;
continue;
}
if(bflags==true)
{
vc_str3++;
vc_str1++;

continue;
}
}
}
jEditor 2003-10-17
  • 打赏
  • 举报
回复
#include <string.h>
using namespace std;
main()
{
string vc_str1 = "1=12345;2=123";
string sTemp = "", vcd_str2"", vc_str3 = "";
int ibegin = 0, ipos = 0, icount = 1;
for(int i = 0; i < vc_str1.length(); i++)
{
ipos ++;
if(vc_str1[i] == ";")
{
icount++;
sTemp = vc_str1.substr(ibegin + 2, ipos - 1);
switch(icount)
{
case 2:
vc_str2 = sTemp;
break;
case 3:
vc_str3 = sTemp;
break;
default:
break;
}
begin = i + 1;
ipos = 0;
}
}
}
zhyy 2003-10-17
  • 打赏
  • 举报
回复
int i;
char vc_str_str[3][128];
char vc_str1[128];
char s[128];
char * ps;
int j;
int off;

vc_str1 = "1=12345;2=123";
ps = NULL;
for (i=1;i<=3;i++)
{
sprintf(s,"%d=",i);
off = strlen(s);
ps = strstr(vc_str1, s);
if (ps == NULL) {vc_str_str[i][0] = 0;continue;}
memset(s, 0, sizeof(s));
for(j=0;;j++)
{
if(ps[j] == 0)
{
ps[0] = 0;
strcpy(vc_str_str[i], ps+off);
}
else if( ps[j] == ';')
{
strncpy(vc_str_str[i], ps+off, j-off-1);
strcpy(s, ps+j+1);
memset(ps, 0, sizeof(vc_str1)-(ps - vc_str1));
strcat(vc_str1, s);
}
else ;
}

}

69,369

社区成员

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

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