简体   繁体   English

如何用 c 中字符串中第二常见的字符替换字符串中最常见的字符?

[英]how to replace the most common character in a string with the second most common in the string in c?

I wrote a C function that receives a string from the user, and replaces the letter with the highest frequency, with the letter with the second highest frequency in the string.我写了一个 C function 从用户那里接收一个字符串,并将频率最高的字母替换为字符串中频率第二高的字母。 Example: For the input i love you more , the string i levo yeu mero will be returned示例:对于输入i love you more ,将返回字符串i levo yeu mero


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

void stringReplace(char str1[]);

int main()
{
    char str1[100] = { 0 };
    stringReplace(str1);
    return 0;
}

void stringReplace(char str1[])
{
    char ch1, ch2;
    int i, h, j, p, n, len, counter1 = 0, counter2 = 0, first, second, times;
    printf("Please enter the string - maximum = 100 characters:\n");
    printf("User input: ");
    fgets(str1, 100, stdin);
    str1[strcspn(str1, "\n")] = 0;
    len = strlen(str1);
    for (i = 0; i < len; i++) {
        counter1 = 0;
        for (j = 0; j < len; j++) {
            if (str1[i] == str1[j]) {
                counter1++;
            }
            if (counter1 > counter2) {
                first = i;
            }
        }
        counter2 = counter1;
    } //character which shows up most - found.

    counter2 = 0;

    for (p = 0; p < len; p++) {
        for (n = 0; n < len; n++) {
            if (str1[p] == str1[n]) {
                counter1++;
            }
            if (counter1 < first && counter1 > counter2) {
                second = p;
            }
        }
        counter2 = counter1;
    }

    ch1 = str1[first];
    ch2 = str1[second];
    
    for (h = 0; h < len; h++) {
        if (str1[h] == ch1) {
            str1[h] = ch2;
        }
    }
    puts(str1);
}

This is the code I have done, and instead of changing the string it prints the same thing.这是我所做的代码,而不是更改字符串它打印相同的东西。 for example:例如:

Please enter the string - maximum = 100 characters:
User input: i love you more
i love you more

Suggest: divide and conquer with helper functions.建议:用辅助函数分而治之

Think about the sub-problem: Counting the 1st & 2nd most popular.想想子问题:计算第 1 和第 2 最受欢迎。

Walk the string.走绳子。 At each character count its occurrence with the rest of the string.在每个字符处,使用字符串的 rest 计算其出现次数。 O(n*n) O(n*n)

When a better than 2nd place count is detected, adjust 1st and 2nd place.当检测到优于第 2 位的计数时,调整第 1 位和第 2 位。

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

void most_popular2(const char *s, char pop[2]) {
  pop[0] = pop[1] = 0;
  size_t pop_count[2] = {0, 0};

  while (*s) {
    // Do not count white-space.
    if (!isspace(*(const unsigned char* )s)) {
      // How popular is *s?
      size_t count = 1;
      // sum all the matches on the rest of the string
      for (const char *r = s + 1; *r; r++) {
        if (*s == *r) {
          count++;
        }
      }

      // Test if better than the 2nd most popular.
      if (count > pop_count[1] && *s != pop[0]) {
        if (count > pop_count[0]) {
          // Demote 1st place to 2nd place.
          pop_count[1] = pop_count[0];
          pop[1] = pop[0];
          // Save new 1st place.
          pop_count[0] = count;
          pop[0] = *s;
        } else {
          // Save new 2nd place.
          pop_count[1] = count;
          pop[1] = *s;
        }

      }
    }
    s++;
  }
}

Sample样本

int main() {
  char s[] = "i love you more i love you more\n";
  char pop[2];
  most_popular2(s, pop);
  printf("1st:%c 2nd:%c\n", pop[0], pop[1]);
}

Output Output

1st:o 2nd:e

Leave it to OP to take the 2 most popular characters and form the desired output.让 OP 选择 2 个最受欢迎的字符并形成所需的 output。


More efficient code could copy the string, sort it O(n*(lng(n)) and then walk it to find the 2 most popular.更高效的代码可以复制字符串,对它进行 O(n*(lng(n)) 排序,然后遍历它以找到最流行的 2 个。

thanks for the help, now i got it.谢谢你的帮助,现在我明白了。 this is what i did:这就是我所做的:

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

#define ASCII_A 97  // ascii for A is 97 
#define MAXSIZE 50 // max size for string 
#define ALPHABETS 26 //total number of alphabets 

void stringReplace(char s[], int a[]);

int main()
{
    char s[MAXSIZE] = {0};
    int a[ALPHABETS] = {0}
    stringReplace(s);
    return 0;
}
/*
function to replace the most common charcter with the second one
input: the string
output: none
*/
void stringReplace(char s[], int a[])
{
    int i;
    int count = 0;
    int max = 0, max2 = 0;
    int index, index2; 

    printf("Enter the string:\n"); 
    fgets(s, MAXSIZE, stdin);
    s[strcspn(s, "\n")] = 0;

    for(i=0; i<26; i++) // initialize all array elements to zero 
    {
        a[i]=0;
    }       

    for(i=0; i<strlen(s); i++) // loop for traversing the string 
    {
        a[s[i]-ASCII_A]++; // increment by 1 for each occurrence. 
    }

    for(i=0; i< ALPHABETS; i++) //prints alphabets and occurrence. 
    {
        count = a[i];
        if (count > max) 
        {
            max = count;
            index = i;
        }
        if(count < max && count > 1)
        {
            max2 = count;
            index2 = i;
        }   
    }
    printf("%c\n", ASCII_A+index);
    printf("%c\n", ASCII_A+index2);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM