c - 在C中将字符串拆分为数组

标签 c arrays

int main() 
{
    int longNum = 12345, tempNum[5], i;
    for (i = 0; i <= 5; i++)
    {
        tempNum[i] = longNum[i] ; // not valid, how do i make this work?
    }
    printf("%d\n", tempNum);
    return 0;
}

我试图遍历 longNum 的所有数字并将它们插入 tempNum[] 中。

最佳答案

您可以尝试模运算符:

int main() 
{
    int longNum = 12345, tempNum[5], i;

    for (i = 4; i >= 0; i--)
    {
        tempNum[i] = longNum % 10;
        longNum /= 10;
    }

    for( i = 0; i < 5; i++ )
    {
        printf("%d\n", tempNum[i]);
    }
    return 0;
}

关于c - 在C中将字符串拆分为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33577369/

相关文章:

c - 如何重新启动双链表?

c - readlink 系统调用有什么作用?

C API : why are functions using returnParameters with buffer + size instead of returning char*

java - 如何在Java中将字节数组转换为字符串,并跳过所有空字节?

arrays - 对两个索引同步(配对)数组进行排序时,如何维护索引?

c - Windows 上的 Ubuntu 上的 Bash : Signal handler does not work

c - 调试 C 程序

php - 如何循环使用 mySQL 查询以返回数组

Java 将 ByteArray 转换为图像

c++ - 从 C++ dll 返回数组到 matlab