如何在c中读取数字(用空格分隔)成数组 [英] How to read numbers (separated by spaces) into an array in c

查看:139
本文介绍了如何在c中读取数字(用空格分隔)成数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ex:输入为:1 20 30 27 89



arr [0] = 1

arr [1] = 20

arr [2] = 30







等等我怎么样可以这样做吗?

ex: input is : 1 20 30 27 89

arr[0]=1
arr[1]=20
arr[2]=30



and so on how i can do this ?

推荐答案

看看这个:将数字分隔成一个空格分成数组 [ ^ ]


你应该试试例如,

You should try something like,
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
char b[] = "1.5, 2.5,3.5,4.5,5.5,6.5,7.5,,,10.5,11.5,12.5,,,15.5,16.5,17.5,18.5,19.5,20.5\n";
double c[21];
char *pptr = b;
b[strlen(b)-1]=',';
for (int i = 0; i < 21; i++) {
    char *ptr = strchr(pptr, ',');
    if (ptr) {
        *ptr = 0;
        c[i] = atof(pptr);
        pptr = ptr + 1;
        }
    }

for (int i = 0; i < 20; i++){
    fprintf(stdout, "%7.2lf", c[i]);
    }
return 0;
}



-KR


-KR


你只需要使用
yourString.Split(' ');

==>一个字符串数组,然后将它们转换为整数。



对于转换,你应该使用:

==> an array of string, then convert each of them into integers.

For conversion you should use:

int.TryParse()





如果你使用C,你应该阅读下一篇: http://www.cplusplus.com/forum/beginner/87238/ [ ^ ]


这篇关于如何在c中读取数字(用空格分隔)成数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆