如何用c中的字符串中第二个最常见的字符替换字符串中最常见的字符?
how to replace the most common character in a string with the second most common in the string in c?
我写了一个C函数,从用户那里接收一个字符串,把出现频率最高的字母替换成字符串中出现频率第二高的字母。
示例:对于输入 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);
}
这是我完成的代码,它没有更改字符串,而是打印了相同的内容。
例如:
Please enter the string - maximum = 100 characters:
User input: i love you more
i love you more
建议:使用辅助函数分而治之。
思考子问题:计算第 1 和第 2 最受欢迎的。
走字符串。在每个字符处计算它与字符串其余部分的出现次数。 O(n*n)
当检测到比第二名更好的计数时,调整第一名和第二名。
#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++;
}
}
示例
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]);
}
输出
1st:o 2nd:e
将其留给 OP 来获取 2 个最受欢迎的字符并形成所需的输出。
更高效的代码可以复制字符串,将其排序为 O(n*(lng(n)),然后遍历它以找到最流行的 2 个。
感谢您的帮助,现在我明白了。
这就是我所做的:
#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
//function declaration
void stringReplace(char s[], int a[]);
int main()
{
//setting the string and the array of the alphabet
char s[MAXSIZE] = {0};
int a[ALPHABETS] = {0};
//calling the function
stringReplace(s, a);
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 max = 0, max2 = 0;
int len;
char first_max, second_max;
printf("Enter the string:\n");
fgets(s, MAXSIZE, stdin);
s[strcspn(s, "\n")] = 0;
len = strlen(s);
for(i=0; i<26; i++) // initialize all array elements to zero
{
a[i]=0;
}
for(i=0; i<len; i++) // loop for traversing the string
{
a[s[i]-ASCII_A]++; // increment by 1 for each occurrence.
}
for(i=0; i< ALPHABETS; i++)
{
if (a[i] > a[max])
{
max2 = max;
max = i;
}
//If a[i] is in between max and max2 then update max2 //
else if (a[i] > a[max2] && a[i] != a[max])
{
max2 = i;
}
first_max = ASCII_A+max;
second_max = ASCII_A+max2;
}
//////////////////////////////////////////////////////////
for (i = 0; i < len; i++) //replace the charcters
{
// check for first_max and replace
if (s[i] == first_max)
{
s[i] = second_max;
}
// check for second_max and replace
else if (s[i] == second_max)
{
s[i] = first_max;
}
}
printf("Most common: %c\n",first_max );
printf("Second most common: %c\n", second_max);
printf("The new string is\n");
puts(s);
}
我写了一个C函数,从用户那里接收一个字符串,把出现频率最高的字母替换成字符串中出现频率第二高的字母。
示例:对于输入 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);
}
这是我完成的代码,它没有更改字符串,而是打印了相同的内容。 例如:
Please enter the string - maximum = 100 characters:
User input: i love you more
i love you more
建议:使用辅助函数分而治之。
思考子问题:计算第 1 和第 2 最受欢迎的。
走字符串。在每个字符处计算它与字符串其余部分的出现次数。 O(n*n)
当检测到比第二名更好的计数时,调整第一名和第二名。
#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++;
}
}
示例
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]);
}
输出
1st:o 2nd:e
将其留给 OP 来获取 2 个最受欢迎的字符并形成所需的输出。
更高效的代码可以复制字符串,将其排序为 O(n*(lng(n)),然后遍历它以找到最流行的 2 个。
感谢您的帮助,现在我明白了。 这就是我所做的:
#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
//function declaration
void stringReplace(char s[], int a[]);
int main()
{
//setting the string and the array of the alphabet
char s[MAXSIZE] = {0};
int a[ALPHABETS] = {0};
//calling the function
stringReplace(s, a);
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 max = 0, max2 = 0;
int len;
char first_max, second_max;
printf("Enter the string:\n");
fgets(s, MAXSIZE, stdin);
s[strcspn(s, "\n")] = 0;
len = strlen(s);
for(i=0; i<26; i++) // initialize all array elements to zero
{
a[i]=0;
}
for(i=0; i<len; i++) // loop for traversing the string
{
a[s[i]-ASCII_A]++; // increment by 1 for each occurrence.
}
for(i=0; i< ALPHABETS; i++)
{
if (a[i] > a[max])
{
max2 = max;
max = i;
}
//If a[i] is in between max and max2 then update max2 //
else if (a[i] > a[max2] && a[i] != a[max])
{
max2 = i;
}
first_max = ASCII_A+max;
second_max = ASCII_A+max2;
}
//////////////////////////////////////////////////////////
for (i = 0; i < len; i++) //replace the charcters
{
// check for first_max and replace
if (s[i] == first_max)
{
s[i] = second_max;
}
// check for second_max and replace
else if (s[i] == second_max)
{
s[i] = first_max;
}
}
printf("Most common: %c\n",first_max );
printf("Second most common: %c\n", second_max);
printf("The new string is\n");
puts(s);
}