我的C语言公共字母统计代码无法正确运行,请求排查
代码问题排查:统计两个单词的不重复公共字母数量
需求说明
需要实现的功能是:获取用户输入的两个纯大小写字母单词,统计它们之间不重复的公共字母数量。例如输入emirhan和celek,正确输出应为你输入的单词有 2 个不重复的公共字母(公共字母为e和c),但当前代码无法正常运行,以下是问题分析及修正方案。
原代码
#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; }
问题分析
内层循环变量错误
两个分支的内层for循环均错误使用i++代替j++(如for (j = 0; j < length2; i++)),导致j永远无法递增,i会快速超出数组范围,触发死循环或数组越界访问,直接导致程序崩溃。长度计算逻辑错误
length函数用strlen(word)-1计算长度,目的是去除fgets读入的换行符,但这种写法不严谨:如果用户输入的单词长度刚好达到MAX-1,fgets不会读入换行符,此时strlen(word)-1会少算一个字符。正确做法是先检查并去除换行符,再获取长度。重复字母处理逻辑完全错误
当前代码找到公共字母后,遍历后续相同字母就减少计数器,这种逻辑完全不符合“统计不重复公共字母”的需求。例如emirhan和celek都包含两个e,按当前逻辑会先加1再减1,最终e不会被计入公共字母,结果完全错误。函数返回值缺失
equation函数声明为int类型,但没有任何return语句,会导致未定义行为。数组参数声明混淆
length函数的参数声明为char word[30],但main中定义的数组是MAX=40,虽然C语言中数组参数实际是指针,但这种写法容易造成误解,且无实际意义。
修正后的代码
#include<stdio.h> #include<string.h> #define MAX 40 // 去除字符串末尾的换行符,返回单词实际长度 int get_word_length(char word[]) { int len = strlen(word); if (len > 0 && word[len-1] == '\n') { word[len-1] = '\0'; len--; } return len; } // 统计两个单词的不重复公共字母数量(大小写不敏感) int count_unique_common_chars(char x[], char y[], int len_x, int len_y) { int seen_x[26] = {0}; // 标记第一个单词出现的字母 int seen_y[26] = {0}; // 标记第二个单词出现的字母 int count = 0; // 标记第一个单词的字母 for (int i = 0; i < len_x; i++) { if (x[i] >= 'a' && x[i] <= 'z') { seen_x[x[i] - 'a'] = 1; } else if (x[i] >= 'A' && x[i] <= 'Z') { seen_x[x[i] - 'A'] = 1; } } // 标记第二个单词的字母 for (int i = 0; i < len_y; i++) { if (y[i] >= 'a' && y[i] <= 'z') { seen_y[y[i] - 'a'] = 1; } else if (y[i] >= 'A' && y[i] <= 'Z') { seen_y[y[i] - 'A'] = 1; } } // 统计两个集合的交集数量 for (int i = 0; i < 26; i++) { if (seen_x[i] && seen_y[i]) { count++; } } return count; } int main() { char x[MAX]; char y[MAX]; printf("请输入第一个单词:"); fgets(x, sizeof(x), stdin); printf("请输入第二个单词:"); fgets(y, sizeof(y), stdin); int len_x = get_word_length(x); int len_y = get_word_length(y); int common_count = count_unique_common_chars(x, y, len_x, len_y); printf("你输入的单词有 %d 个不重复的公共字母\n", common_count); return 0; }
内容的提问来源于stack exchange,提问作者Emirhan CELIK
相关产品推荐
相关产品推荐

