gpt4 book ai didi

c - 嵌套 for 循环不迭代

转载 作者:行者123 更新时间:2023-11-30 20:05:21 25 4
gpt4 key购买 nike

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

下面的比较函数的目标是将第一个字符串中的每个数组元素与第二个字符串中的每个数组元素进行比较。如果我们找到了匹配项,那么我们会“举起红旗”(充当 bool 值),并且我们不会将其添加到将包含编辑后的 ​​string1 的新数组中。然而,它似乎忽略了第二个 for 循环。它仅在每次 i 迭代的 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.

最佳答案

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])

使用==而不是=(这是赋值运算符)。所以条件应该写成-

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

类似地在这种情况下(正如Weather Vane Sir在评论中提到的)if((k=(s2Length-1))&&(redFlagRaished = 0)) 使用==

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

关于c - 嵌套 for 循环不迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32078851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com