C语言 如何计算一个字符串中有多少个香蕉,但其中一个字母必须大写?

nwlls2ji  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(98)

我应该做一个函数来计算单词“banana”在输入字符串中出现了多少次。如果没有,函数应该返回值零,如果有一个或多个,函数应该返回相应的数字。此外,单词“banana”必须有一个大写字母,不管是哪一个。程序实际上不必计算单词banana本身,而是计算字符串中是否有字母可以组成“banana”,而你可以组成多少个“banana”就是返回的数字。
我已经写了代码,但函数总是返回值0。

#include <stdio.h>

void getInput(char array[], int size) {
    char character = getchar();
    
    if (character == '\n') {
        character = getchar();
    }

    int i = 0;
    while (i < size - 1 && character != '\n') {
        array[i] = character;
        i++;
        character = getchar();
    }
    array[i] = '\0';
}

int countBananas(const char *str) {
    int i;
    int countA = 0, countB = 0, countN = 0, uppercaseCount = 0, A = 0, B = 0, N = 0;

    for (i = 0; str[i] != '\0'; i++) {
        if (str[i] == 'A' || str[i] == 'a') {
            countA++;
            if (str[i] == 'A') {
                uppercaseCount++;
            }
        }

        if (str[i] == 'B' || str[i] == 'b') {
            countB++;
            if (str[i] == 'B') {
                uppercaseCount++;
            }
        }

        if (str[i] == 'N' || str[i] == 'n') {
            countN++;
            if (str[i] == 'N') {
                uppercaseCount++;
            }
        }
    }

    if (countB >= 1 && countA >= 3 && countN >= 2 && uppercaseCount != 0) {
        if (countB % 1 == 0 && countA % 3 == 0 && countN % 2 == 0) {
            countA = 0, countB = 0, countN = 0;

            for (i = 0; str[i] != '\0'; i++) {
                if (str[i] == 'A' || str[i] == 'a') {
                    countA++;
                    if (countA == 3) {
                        A++;
                        countA = 0;
                    }
                }

                if (str[i] == 'B' || str[i] == 'b') {
                    countB++;
                    if (countB == 1) {
                        B++;
                        countB = 0;
                    }
                }

                if (str[i] == 'N' || str[i] == 'n') {
                    countN++;
                    if (countN == 2) {
                        N++;
                        countN = 0;
                    }
                }
            }

            if (A < B && A < N) {
                return A;
            } else if (B < A && B < N) {
                return B;
            } else if (N < A && N < B) {
                return N;
            } else if (A == B && A == N) {
                return A;
            }
        } else {
            return 0;
        }
    } else {
        return 0;
    }
    return 1;
}

int main() {
    char str[100];
    printf("Enter a string: ");
    getInput(str, 100);

    int result = countBananas(str);
    printf("%d", result);

    return 0;
}

字符串

6mzjoqzu

6mzjoqzu1#

这就是如何实现这一目标,如果我们被允许绕过元素:

#include <stdio.h>
#include <ctype.h>

int bananaSearcher(char * input, char *bananaString, int inputIndex, int alreadyCapital) {
    if (alreadyCapital && (*bananaString == '\0')) {
        return 1;
    }
    int counter = 0;
    char current;
    while ((current = *(input + inputIndex++)) != '\0') {
        if ((current == *bananaString) || ((!alreadyCapital) && (current == toupper(*bananaString)))) {
            counter += bananaSearcher(input, bananaString + 1, inputIndex, alreadyCapital || (current == toupper(*bananaString)));
        }
    }
    return counter;
}

int main() {
    char * banana = "banana";
    char * input = "B32ananaBbAananadskljsd";
    printf("%d", bananaSearcher(input, banana, 0, 0));
    return 0;
}

字符串
对于B32ananaBbAananadskljsd的输入,输出为34,具有以下变化:

0 3 4 5 6 7 
B a n a n a 
0 3 4 5 6 11 
B a n a n a 
0 3 4 5 6 13 
B a n a n a 
0 3 4 5 6 15 
B a n a n a 
0 3 4 5 12 13 
B a n a n a 
0 3 4 5 12 15 
B a n a n a 
0 3 4 5 14 15 
B a n a n a 
0 3 4 7 12 13 
B a n a n a 
0 3 4 7 12 15 
B a n a n a 
0 3 4 7 14 15 
B a n a n a 
0 3 4 11 12 13 
B a n a n a 
0 3 4 11 12 15 
B a n a n a 
0 3 4 11 14 15 
B a n a n a 
0 3 4 13 14 15 
B a n a n a 
0 3 6 7 12 13 
B a n a n a 
0 3 6 7 12 15 
B a n a n a 
0 3 6 7 14 15 
B a n a n a 
0 3 6 11 12 13 
B a n a n a 
0 3 6 11 12 15 
B a n a n a 
0 3 6 11 14 15 
B a n a n a 
0 3 6 13 14 15 
B a n a n a 
0 3 12 13 14 15 
B a n a n a 
0 5 6 7 12 13 
B a n a n a 
0 5 6 7 12 15 
B a n a n a 
0 5 6 7 14 15 
B a n a n a 
0 5 6 11 12 13 
B a n a n a 
0 5 6 11 12 15 
B a n a n a 
0 5 6 11 14 15 
B a n a n a 
0 5 6 13 14 15 
B a n a n a 
0 5 12 13 14 15 
B a n a n a 
0 7 12 13 14 15 
B a n a n a 
0 11 12 13 14 15 
B a n a n a 
8 11 12 13 14 15 
B a n a n a 
9 10 12 13 14 15 
b A n a n a


对于BAnanabanana的输入,输出为10,可能性为:

0 3 4 5 8 9 
B a n a n a 
0 3 4 5 8 11 
B a n a n a 
0 3 4 5 10 11 
B a n a n a 
0 3 4 7 8 9 
B a n a n a 
0 3 4 7 8 11 
B a n a n a 
0 3 4 7 10 11 
B a n a n a 
0 3 4 9 10 11 
B a n a n a 
0 3 8 9 10 11 
B a n a n a 
0 5 8 9 10 11 
B a n a n a 
0 7 8 9 10 11 
B a n a n a


说明:

  • 我们有input,一个字符串
  • bananaString,其中我们预先填充小写“banana”字符串
  • 以及inputIndex,所以我们知道我们在哪里
  • alreadyCapital,以记录只有一个字母是大写的标准
  • 如果我们到达bananaString的末尾,并且我们只有一个大写字母(否则我们不会到达这一点),那么我们有一个解决方案,我们停止
  • 否则
  • 我们用0初始化counter,这是加法算术运算的中性元素
  • 我们创建current,一个我们缓存当前值的字符
  • 如果current匹配bananaString的当前期望字符(如果我们已经有了大写字母,那么我们区分大小写,否则以不区分大小写的方式进行)
  • 然后我们调用bananaSearcher,并将解的数量添加到counter
  • 我们通过input
  • bananaString的下一个位置,因为我们找到了当前位置
  • 递增的inputIndex,这样我们就可以知道我们在哪里
  • 我们计算是否已经有了大写字母
wxclj1h5

wxclj1h52#

实现@n.m.couldbeanAI共享的算法。我们计算了很多次,无论大小写,我们都有预期的字母频率,但确保它不长于大写字母的数量。

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

#define LEN 100
#define LETTERS "ABNabn"

int min(size_t n, int *vals) {
    int min = *vals;
    for(size_t i = 1; i < n; i++)
        if(vals[i] < min)
            min = vals[i];
    return min;
}

void str_freq(const char *str, const char *letters, int *freq) {
    memset(freq, 0, sizeof *freq * strlen(letters));
    for(; *str; str++) {
        char *found = strchr(letters, *str);
        if(found)
            freq[found - letters]++;
    }
}

int main(void) {
    char str[LEN];
    if(!fgets(str, LEN, stdin))
        return 1;
    int freq[sizeof LETTERS - 1];
    str_freq(str, LETTERS, freq);
    printf("%d\n",
        min(2, (int []) {
            min(3, (int []) {
                (freq[0] + freq[3]) / 3, // [Aa]
                freq[1] + freq[4],       // [Bb]
                (freq[2] + freq[5]) / 2  // [Nn]
            }),
            freq[0] + freq[1] + freq[2]  // [ABN]
        })
    );
}

字符串
示例运行:

$ ./a.out
banana
0
$ ./a.out
Banana
1
$ ./a.out
BAnanabanana
2

yzuktlbb

yzuktlbb3#

我是这样解决的:

#include <stdio.h>  
    void inputString(char array[], int size) {
        char character = getchar();
        if (character == '\n') character = getchar();
        int i = 0;
        while (i < size - 1 && character != '\n') {
            array[i] = character; // STRING INPUT
            i++;
            character = getchar();
        }
        array[i] = '\0';
    }
    
    int countBananas(const char *str) {
        int i;
        int letterA = 0, letterB = 0, letterN = 0, uppercaseLetter = 0, uppercaseUsed = 0, sets = 0;
    
        for (i = 0; str[i] != '\0'; i++) {
            if (str[i] == 'A' || str[i] == 'a') {
                letterA++;
    
                if (str[i] == 'A') {
                    uppercaseLetter++;
                }
            }
    
            if (str[i] == 'B' || str[i] == 'b') {
                letterB++;
                if (str[i] == 'B') {
                    uppercaseLetter++;
                }
            }
    
            if (str[i] == 'N' || str[i] == 'n') {
                letterN++;
    
                if (str[i] == 'N') {
                    uppercaseLetter++;
                }
            }
    
            if (str[i] >= 'A' && str[i] <= 'Z') {
                uppercaseUsed = 1;
            }
        }
    
        if (letterB >= 1 && letterA >= 3 && letterN >= 2 && uppercaseLetter != 0 && uppercaseUsed) {
            while (letterA >= 3 && letterB >= 1 && letterN >= 2) {
                letterA -= 3;
                letterB -= 1;
                letterN -= 2;
                sets++;
            }
            return sets;
        } else {
            return 0;
        }
    }
    
    int main() {
        char input[100];
        printf("Enter some string: ");
        inputString(input, 100);
    
        int result = countBananas(input);
        printf("%d", result);
    
        return 0;
    }

字符串

相关问题