c - 在 C 中使用带有数组的循环

标签 c arrays string loops char

#include <stdio.h>    

void main()
{
    char couples[3][50] = { "Paul Kathy", "John Nora", "Martin Mary" };
    char husbands[3][50];
    char wives[3][50];  
    int i = 0;
    int j = 0;

    puts("Husbands: ");
    for (i = 0; i < 3; i++)
    {
        while (couples[i][j] != ' ')
        {
            couples[i][j] = husbands[i][j];
            j++;
        }
        husbands[i][j] = '\0';
        puts(husbands[i]);
        j = 0;
    }

}

我以前创建过一个与此类似的程序,并且运行得很好。然而,虽然这确实成功构建和编译,但它无法正确运行。本质上,我试图根据空格字符将一对分隔成一个单独的字符串。我做错了什么?

最佳答案

这是你的问题:

couples[i][j] = husbands[i][j];

您正在将丈夫分配给夫妻,而不是按照您的需要反过来。

关于c - 在 C 中使用带有数组的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40184362/

相关文章:

c# - 将单词拆分为一组唯一字母

c - 当我尝试从字符串结构中获取数据时应用程序卡住?

c# - 检查字符串是否仅包含 C# 中的数字的最快方法

c - 关于栈指针的疑问

c - Eventloop 具有高 ksoftirqd 负载; nginx 不会,但会执行相同的系统调用。为什么?

python - 有选择地否定数组中的元素

objective-c - 在添加到数组之前格式化为小数点后2位

mysql - 在 MySQL 上用 pdo 替换字符串

c++ - 在 cygwin 中使用 make : "cannot execute binary file" 编译的 C 程序

c - 如何在 Mac 上使用 C 管道?