嵌套for循环不是迭代

时间:2015-08-18 17:23:09

标签: c for-loop

我正在尝试K和R练习。该程序是比较两个字符串。如果第一个字符串具有也在字符串2中的任何字符,则它将在string1中删除。

下面我的比较函数的目标是将第一个字符串中的每个数组元素与第二个字符串中的每个数组元素进行比较。如果我们有一个匹配,那么我们“引发一个红旗”(作为一个布尔值),我们不要将它添加到将包含已编辑的string1的新数组。然而它似乎忽略了第二个for循环。它只在每次迭代时通过k = 0迭代。我的另一个问题是基于输出(在节点下面提供),似乎s1 [i]被分配给s2 [k]。我猜这发生在if语句中但是怎么可能呢?任何人都可以提供的帮助将非常感激。

如果它有所作为,我使用了GNU GCC编译器。

#include <stdio.h>

int getLength(char s[]);
char compare(char s1[], char s2[],int s1Length, int s2Length);

int main()
{
    char stringOne[] = {'a','b','c','d','e'};
    char stringTwo[] = {'P','f','g','c','t','y','u','o','z'};
    int lengthOne;
    int lengthTwo;
    lengthOne = getLength(stringOne);
    char theResultingString[lengthOne];

    lengthTwo = getLength(stringTwo);
    compare(stringOne, stringTwo, lengthOne, lengthTwo);

    return 0;
}               //end of main.

int getLength(char s[])     //getLength gives us the length of each and every string
{
    int i=0;
    for(i = 0; s[i]!='\0'; i++) {
    }               //end for loop
    return i;
}               //end of getLength

char compare(char s1[], char s2[],int s1Length, int s2Length)
{
    int redFlagRaised = 0;      //This will be used as a boolean indicator if we have a matching element
    char toBeReturned[s1Length];
    int i;
    int k;

    for(i = 0; i<s1Length; i++) {
        printf("i is now %d\n",i);
        for(k = 0; k<s2Length; k++) {
            printf("k is now %d\n",k);

            if(s1[i] = s2[k]) {     //If at any point the s1 char being examined equals any of s2 chars then

                printf("s1[i] is %c\n",s1[i]);
                printf("s2[i] is %c\n",s2[i]);

                redFlagRaised = 1;      //we raise the red flag!
            }               //end first inner if statement

            if((k=(s2Length-1))&&(redFlagRaised = 0)) { //if we reach the end and we DON'T have a red flag then
                toBeReturned[i] = s1[i];
                printf("toBeReturned[0] is %c\n",toBeReturned[0]);
            }               //end second inner if statement


        }               //end inner for loop
        redFlagRaised = 0;      //We lower the flag again for the next inner for loop iteration

    }               //end outer for loop

    printf("The result is %c", toBeReturned[0]);
    return toBeReturned[0];
}               //end of compare


输出:

i is now 0
k is now 0
s1[i] is P
s2[i] is P
i is now 1
k is now 0
s1[i] is P
s2[i] is f
i is now 2
k is now 0
s1[i] is P
s2[i] is g
i is now 3
k is now 0
s1[i] is P
s2[i] is c
i is now 4
k is now 0
s1[i] is P
s2[i] is t
i is now 5   
k is now 0
s1[i] is P
s2[i] is y
The result is �
Process returned 0 (0x0)   execution time : 0.005 s
Press ENTER to continue.

2 个答案:

答案 0 :(得分:3)

char stringOne[] = {'a','b','c','d','e'};
char stringTwo[] = {'P','f','g','c','t','y','u','o','z'};

这些不是字符串。您需要使用null character终止它们。 试试这个 -

 char stringOne[] = {'a','b','c','d','e','\0'};
 char stringTwo[] = {'P','f','g','c','t','y','u','o','z','\0'};

同样在这种情况下 -

if(s1[i] = s2[k])

使用==代替=(这是赋值运算符)。条件应写为 -

if(s1[i]==s2[k])

同样在这种情况下(如评论中的Weather Vane Sir所述)if((k=(s2Length-1))&&(redFlagRaised = 0))使用==

 if((k==(s2Length-1))&&(redFlagRaised == 0))

答案 1 :(得分:0)

在IF条件下的比较函数中,您将值分配给K,如bleow

if((k=(s2Length-1))&&(redFlagRaised = 0)){  //if we reach the end and we DON'T have a red flag then
toBeReturned[i] = s1[i];
printf("toBeReturned[0] is %c\n",toBeReturned[0]);
} 

但它必须像这样

if((k==(s2Length-1))&&(redFlagRaised == 0)){  //if we reach the end and we DON'T have a red flag then
toBeReturned[i] = s1[i];
printf("toBeReturned[0] is %c\n",toBeReturned[0]);
} 

您必须使用比较运算符(==)而不是赋值运算符(=)

在以下代码中

char stringOne[] = {'a','b','c','d','e'};
char stringTwo[] = {'P','f','g','c','t','y','u','o','z'};

这些不是字符串。您需要使用空字符终止它们。试试这个 -

 char stringOne[] = {'a','b','c','d','e','\0'};
 char stringTwo[] = {'P','f','g','c','t','y','u','o','z','\0'};

下面也使用此==运算符而不是=运算符

if(s1[i] = s2[k])