C语言 为什么不工作我的代码,即使它是真的?

pkwftd7m  于 2023-01-20  发布在  其他
关注(0)|答案(1)|浏览(91)

这个代码首先为用户取两个单词。2然后,它用一个函数检查这些单词的长度。3然后代码把单词和它们的长度发送到另一个函数,让它在这些单词中找到常用字母。4同样的函数还打印出它们有多少个常用字母。5(用户只输入小写字母或大写字母)
例如:
投入1:埃米尔汗
输入2:celek
输出:您输入的单词有2个常用字母
output不应该是3,因为input2(celek)有2个'e',而代码计数只是其中之一。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 40

int length(char word[30]) {

    int uzun;
    uzun = strlen(word) - 1;
    return uzun;

}

int equation(char x[MAX], char y[MAX], int length1, int length2) {

    int i, j, k, l;
    int sayac = 0; // sayac  means counter

    if (length1 >= length2) {
        for (i = 0; i < length1; i++) {
            for (j = 0; j < length2; i++) {
                if (x[i] == y[j]) { // if the code find a common letter,it will enter if
                    sayac++;
                    for (k = i + 1; k < length1; k++) { //This 'for' check that does first word same letter or not?
                        if (x[k] == x[i]) {
                            sayac--;
                        }
                    }
                    for (l = j + 1; l < length2; l++) { //This 'for' check that does second word same letter or not?
                        if (y[l] == y[j]) {
                            sayac--;
                        }
                    }
                }

            }
        }
    }

    if (length1 < length2) {
        for (i = 0; i < length2; i++) {
            for (j = 0; j < length1; i++) {
                if (x[i] == y[j]) {
                    sayac++;
                    for (k = i + 1; k < length2; k++) {
                        if (x[k] == x[i]) {
                            sayac--;
                        }
                    }
                    for (l = j + 1; l < length1; l++) {
                        if (y[l] == y[j]) {
                            sayac--;
                        }
                    }
                }

            }
        }
    }
    printf("The words that you entered have %d common letters", sayac);
}

int main() {
    char x[MAX]; //input1
    char y[MAX]; //input2

    int length1; // length of first word
    int length2; // length of second word

    printf("PLS, enter the first word:");
    fgets(x, sizeof(x), stdin);
    printf("PLS, enter the second word:");
    fgets(y, sizeof(y), stdin);

    length1 = length(x);
    length2 = length(y);

    equation(x, y, length1, length2);

    return 0;
}
yeotifhr

yeotifhr1#

我再次编辑了我的代码,我解决了这个问题。我在循环中有一个错误。我再次编码循环。

#include<stdio.h>
#include<stdlib.h>
#include<string.h> 

#define MAX 40

int length(char word[30]) {

    int uzun;
    uzun = strlen(word) - 1;
    return uzun;
    
}

void equation(char x[MAX], char y[MAX], int length1, int length2) {

    int i, j, k, l;
    int sayac = 0; // sayac  means counter

        for (i = 0; i < length1; i++) {
            for (j = 0; j < length2; j++) {
                if (x[i] == y[j]) { // if the code find a common letter,it will enter if
                    sayac++;
                    for (k = i + 1; k < length1; k++) { //This 'for' check that does first word same letter or not?
                        if (x[k] == x[i]) { 
                            sayac--;
                        }
                    }
                    for (l = j + 1; l < length2; l++) { //This 'for' check that does second word same letter or not?
                        if (y[l] == y[j]) {
                            sayac--;
                        }
                    } 
                }

            }
        }
    

    
    printf("The words that you entered have %d common letters", sayac);
} 

int main() {
    char x[MAX]; //input1
    char y[MAX]; //input2

    int length1; // length of first word
    int length2; // length of second word

    printf("PLS, enter the first word:");
    fgets(x, sizeof(x), stdin);
    printf("PLS, enter the second word:");
    fgets(y, sizeof(y), stdin);

    length1 = length(x);
    length2 = length(y);

    equation(x, y, length1, length2);

    return 0;
}

相关问题