日期:2014-05-16  浏览次数:20618 次

在线等,关于取得分隔字符串的问题,给分
比如有一个字符串 S0101001_200809
我要取得分隔线前后的两个字符,如何做啊?

#include <string.h>
#include<stdio.h>

int main(void)
{
  char string[16];
  char start_time[16];
  char kikai_num[16];
  char * s,*t, c = '_';
  int scnt = 0;
  int tcnt = 0;

  strcpy( string, "S0101002_200812");
   
  s = strrchr(string,'_');
   
  scnt = strlen(s+1);
  memcpy(start_time,s+1,scnt);

|

这样只取得了后面的,如何取得 “_”前面的字符串阿,急~~~~~~~~写了,给分的

------解决方案--------------------
#include <stdio.h>
#include <string.h>


int main()
{
char str[] = "S0101001_200809";
char front[16] = {0};
char rear[16] = {0};
char *p;

p = strtok(str, "_");
strcpy(front, p);
p = strtok(NULL, "_");
strcpy(rear, p);

printf("%s\n", front);
printf("%s\n", rear);
return 0;


}
------解决方案--------------------
#include <string.h>
#include <stdio.h>

int main(void)
{
char string[16];
char start_time[16] = {0};
char kikai_num[16] = {0};
char *s, c = '_'; 
int scnt = 0;
int tcnt = 0;

strcpy( string, "S0101002_200812");

s = strrchr(string,'_');

scnt = strlen(s+1);
memcpy(kikai_num,string,s - string);
memcpy(start_time,s+1,scnt);
printf("kikai_num:%s\n",kikai_num);
printf("start_time:%s\n",start_time);

return 0;
}