如何设置仅扫描N次的循环

时间:2013-11-24 17:39:04

标签: c string string-comparison

我是编程的新手,所以请尽可能简单地理解答案,谢谢! 在我尝试的程序中,我需要它根据用户输入的数字“N”扫描不同数量的字符串(N),并比较字符串以查看它们是否相等。

我以为我会使用for循环,但这似乎不起作用。

printf("How many rows will there be?\n");
    scanf("%d", &N);

    for (j = 0; j < N; j++)
        scanf("%s", &string1[N]);
        scanf("%s", &string2[N]);
        result = strncmp(string1, string2, compareLimit);

是我现在拥有的,但我想重复“scanf(”%s“,&amp; string1 [N]);”排N次。任何建议?非常感谢!!!

1 个答案:

答案 0 :(得分:0)

printf("How many rows will there be?\n");
scanf("%d", &N);
/* GET MEMORY FOR N STRING VARIABLES */

for (j = 0; j < N; j++)

    {
     scanf("%s", &string1[N]); /* TOOK INPUT N TIMES BUT ..ITS OVERWRITING ONLY */
     scanf("%s", &string2[N]);
    }
    result = strncmp(string1, string2, compareLimit); /*U WANT TO COMPARE AT EACH RUN OF                      LOOP THEN INCLUDE THIS ALSO,IT  RETURNS 0 IF SAME */

你的问题/程序不清楚,所以很难说你想知道什么。

相关问题