你怎么能拆分这么复杂的字符串

时间:2020-12-21 18:19:29

标签: c

输入

c 中的字符串如下所示:“12345 computer seince 101”

输出

课程数量 = "12345"

课程名称 = "computer seince 101"

我需要忽略数字和名称之前的所有空格键,因此允许使用以下字符串:“(空格)12345(更多空格)计算机科学 101”

我尝试使用 strtok,但我失败了

1 个答案:

答案 0 :(得分:0)

使用 strtol() 函数。

#include <stdio.h>
#include <stdlib.h>

int main () {
   char str[30] = " 12345 computer science 101";
   char *ptr;
   long ret;

   ret = strtol(str, &ptr, 10);
   printf("NUMBER OF COURSE %ld\n", ret);
   printf("NAME OF COURSE %s", ptr);

   return(0);
}

输出为:

NUMBER OF COURSE 12345
NAME OF COURSE  computer science 101