关键词:strtok(),atoi()

 

对于一些变态的输入:

  输入 n 个数,输入的格式为每两个数间有',';

  例如:

  input:

  n=10

  1,2,3,4,5,6,7,8,9,10

  你可能只需要数字,而不需要',',那么,该如何从中分解出数字呢?

1.下面介绍strtok()函数:

  字符串分解

2.字符串与数字间的相互转化函数

  字符串分解

 1 void Test()
 2 {
 3     //假设输入 n 个数,每两个数间都会有','隔开
 4     //例如 n = 10 "1,2,3,4,5,6,7,8,9,10"
 5 
 6     int n=10;
 7     int num[15];
 8     strcpy(buf,"1,2,3,4,5,6,7,8,9,10\0");
 9 
10     p=strtok(buf,",");
11     num[1]=atoi(p);
12     for(int i=2;i <= n;++i)
13     {
14         p=strtok(NULL,",");
15         num[i]=atoi(p);
16     }
17     for(int i=1;i <= n;++i)
18         printf("%d ",num[i]);
19     printf("\n");
20 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2021-12-07
  • 2021-06-28
  • 2022-01-27
猜你喜欢
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2021-08-27
  • 2022-12-23
相关资源
相似解决方案