簡體   English   中英

交換字符串中兩個最頻繁出現的字母

[英]Swap occurrences of two most frequent letters in a string

我不知道代碼中的問題是什么,但是當我編譯時會得到:

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;

但是,第一個調用將第一次出現的letter2letter3 第二個呼叫改變的第一次出現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的“這里字符串”來提供數據— 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]
$

strcspn(const char * str1,const char * str2)在第一個字符串的第二個字符串中查找任何字符的第一個實例。 您將傳遞字符而不是字符串作為第二個參數。 您需要函數strchr(const char * string,int character),該函數查找單個字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM