gpt4 book ai didi

c - 交换字符串中出现频率最高的两个字母

转载 作者:太空宇宙 更新时间:2023-11-04 06:58:10 24 4
gpt4 key购买 nike

我不知道我的代码有什么问题,但是当我编译时我得到:

warning: passing arg 2 of `strcspn' makes pointer from integer without a cast

代码如下:

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

#define STR_LEN 50

int main(void) {
int i = 0, j = 0, length = 0, count1 = 0, count2 = 0, count3 = 0;
char letter3 = 'a', letter2 = 'a', string[STR_LEN] = { 0 };

length = strlen(string);
printf("Enter a sentence: ");
fgets(string, STR_LEN, stdin);

for (i = 0; i < length; i++) {
for (j = 0; j < length; j++) {
if (string[i] == string[j]) {
count1++;
} else {
count1 = 0;
}
}
if (count1 > count3) {
count2 = count3;
count3 = count1;
letter2 = letter3;
letter3 = string[i];
} else
if (count1 > count2) {
count2 = count1;
letter2 = string[i];
}
}

string[strcspn(string, letter2)] = letter3;
string[strcspn(string, letter3)] = letter2;

printf("\n %s", string);

system("pause");

return 0;
}

代码应该从用户那里得到一个句子,并将句子中最常见的字母切换为第二个常见的字母。

最佳答案

眼前的问题

strcspn() 函数将两个字符串作为参数,但您传递的是一个字符串和一个字符。您需要以某种方式将字符转换为字符串。一种方法是:

int sep[2] = "";
sep[0] = letter2;
string[strcspn(string, sep)] = letter3;
sep[0] = letter3;
string[strcspn(string, sep)] = letter2;

但是,第一次调用将第一次出现的 letter2 更改为 letter3;第二次调用将第一次出现的 letter3(可能是上次调用中刚刚替换的那个)更改为 letter2。这不是转换字符串的完整工作——您需要扫描整个字符串以进行更改。

实现解决方案

一种可能是这样的:

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

#define NULL_VALUE '\0'

static inline void map(char *str, int len, int c_old, int c_new)
{
for (int i = 0; i < len; i++)
{
if (str[i] == c_old)
str[i] = c_new;
}
}

int main(void)
{
char buffer[4096];

printf("Enter a sentence: ");
if (fgets(buffer, sizeof(buffer), stdin) == 0)
return 0;
int length = strlen(buffer);
if (length > 0)
buffer[--length] = '\0';

putchar('\n');
printf("Original [%s]\n", buffer);

int count[256] = { 0 };
for (int i = 0; i < length; i++)
{
if (isalpha((unsigned char)buffer[i]))
count[(unsigned char)buffer[i]]++;
}

int max1_count = 0;
int max2_count = 0;
char max1_value = '\0';
char max2_value = '\0';
for (int i = 0; i < 256; i++)
{
if (count[i] > max1_count)
{
max2_count = max1_count;
max2_value = max1_value;
max1_count = count[i];
max1_value = i;
}
else if (count[i] > max2_count)
{
max2_count = count[i];
max2_value = i;
}
}

/*
** Since a string is a sequence of non-null character codes followed
** by a null byte, it is safe to use '\0' as the temporary value in
** the three-step swap operation
*/
if (max2_count > 0)
{
map(buffer, length, max1_value, NULL_VALUE);
map(buffer, length, max2_value, max1_value);
map(buffer, length, NULL_VALUE, max2_value);
}

printf("Revised [%s]\n", buffer);

return 0;
}

使用宏 NULL_VALUE 的唯一原因是三个 map() 线的对称性是不言而喻的。

示例运行

我调用了程序 ccswap19,我使用 Bash 'here strings' 来提供数据 — putchar('\n'); 表示输出出现在与提示分开的一行上。如果您以交互方式运行该程序,则在“原始”打印之前会有一个空行。

$ ccswap19 <<< "The hidden costs of the exodus are now revealed for all to see."
Enter a sentence:
Original [The hidden costs of the exodus are now revealed for all to see.]
Revised [Tho hiddon cests ef tho oxedus aro new rovoalod fer all te soo.]
$ ccswap19 <<< "aaaaaaaaaaaa"
Enter a sentence:
Original [aaaaaaaaaaaa]
Revised [aaaaaaaaaaaa]
$ ccswap19 <<< "aaaabaaaaaaa"
Enter a sentence:
Original [aaaabaaaaaaa]
Revised [bbbbabbbbbbb]
$

关于c - 交换字符串中出现频率最高的两个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41664603/

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