有没有办法在 C 中一次获得一个字符?

时间:2020-12-29 10:53:35

标签: c string char character

我有一个这种类型的字符串: %d.'\0' 我想解析它。例如,如果有这个字符串 12.'\0' 我想逐个字符地读取字符串(在点之前)并在整数变量中添加数字 12

编辑:所以我有两个进程通过消息队列进行通信。客户端发送如下消息:12.5,我必须将 12 与 5 分开。我不想输入。我在字符串变量中有字符串 (12.5) 并且需要解析它,因为我想要某个 int 变量中的 12 和另一个中的 5。

1 个答案:

答案 0 :(得分:0)

我会使用函数 strtok 这就像其他语言中的函数拆分 和用于将字符串更改为 int 的函数 atoi

char e[] = "12.5";
char * num = strtok(e,".");
// x is the first number 
int x = atoi(num);
printf("%d\n",x);
// y is the number after the dot
num = strtok(NULL,".");
int y = atoi(num);
相关问题