【问题标题】:How to replace every specific character to another in a string C?如何将字符串 C 中的每个特定字符替换为另一个字符?
【发布时间】:2013-12-31 15:10:21
【问题描述】:

我用 C 编程。我应该创建一个程序来识别字符串中最常见的字符和第二常见的字符。

我不知道为什么,但它不起作用。程序应该把它的位置放入一个整数中。不是指针,但如果最常见的是str1[i],那么它将把i 的值放入一个整数中。所以在第二个最常见的。如果是str1[j],则应该将j 的值放入一个整数中。然后,它应该用第二常见的替换最常见的。替换功能有效,循环中可能存在问题,虽然我无法弄清楚它是什么。

这是我所拥有的(假设所有的整数和字符串都在开头声明):

void stringReplace(char str1[], char ch1, char ch2);

int main()
{
    char str1[100];
    char ch1, ch2;
    int i, j, p, n, len, counter1, counter2, first, second, times;
    printf("Please enter the string - maximum = 100 characters:\n");
    printf("User input: ");
    gets(str1);
    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];
    stringReplace(str1, ch1, ch2);
    puts(str1);
    return 0;
}

void stringReplace(char str1[], char ch1, char ch2)
{
    int i, j, len;
    len = strlen(str1);
    for(i = 0 ; i <= len ; i++)
    {
        if(str1[i] == ch1)
        {
            str1[i] = ch2;
        }
    }
}

问题出在哪里?

【问题讨论】:

  • 如果len 是字符串的实际长度,那么一个问题是你的for 循环应该有&lt; len 而不是&lt;= len。否则,您将遍历 len+1 字符并处理空终止符(如果有的话)。
  • 已完成但仍无法正常工作:(我将编辑整个程序以便您理解 :P
  • 正如我在评论中提到的,这只是“一个问题”。可能还有其他人。此外,您并没有解决所有问题。 ;)
  • 什么不起作用?程序哪里出错了?程序在哪里正常运行?
  • 替换之类的都不错。但是,如果我输入例如“I love you more”(应该输出 I leve yeu mere),那么它会输出“I lov you mor”。

标签: c string loops for-loop


【解决方案1】:

所以你想在一个字符串中找到 n 个最大数字,即 n=2,对吧? 我为你做了一个小例子。代码与您的略有不同,但运行良好。

#include <stdio.h>

int main(int argc, char* argv[])
{
    char str[] = "Algorithms Are Funnnn!\0";
    int i=0;
    int offset=33;
    int ocurrs[94] = {0}; //considering from 33 to 126 (valid chars - ASCII Table [0-127]).
    int max[2]={0};

    while(str[i]) 
        ocurrs[str[i++]-offset]++;

    for(i=0; i<94; i++)
        if(ocurrs[i]>ocurrs[max[1]]){
           max[0] = max[1]; 
           max[1] = i;
        }
        else if(ocurrs[i]>ocurrs[max[0]]) 
           max[0]=i;

    printf("chars '%c'(%d times) and '%c'(%d times) occurred most.\n",
        offset+max[1], ocurrs[max[1]], offset+max[0], ocurrs[max[0]]);

    return 0;
}

另外,尽量远离 get,因为它完全不安全。 如果您想最多获取 100 个字符,请改用它:

char buffer[100];
fgets(buffer, 100, stdin);

问候。

【讨论】:

  • 你说的是 max[2] 吗?
【解决方案2】:

无法抗拒通过一些循环找到答案,没有填充出现技术。

编码非常有趣。

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

int main( void ) {

    char szInput[] = "ONE DOES NOT SIMPLY WALK INTO MORDOR!";
    int len = strlen( szInput );

    int MaxCountSoFar_1 = 0;
    int MaxIndexSoFar_1 = -1;
    int MaxCountSoFar_2 = 0;
    int MaxIndexSoFar_2 = -1;
    int i, j, CountThatOne;

    for ( i = 0; i < len; ++i ) {
        if ( szInput[ i ] == ' ' ) continue;
        // count that char
        CountThatOne = 1;
        // don't start from 0, they are already "counted"
        for ( j = i + 1; j < len; ++j ) {
            if ( szInput[ i ] == szInput[ j ] ) ++CountThatOne;
        }
        if ( CountThatOne > MaxCountSoFar_1 ) {
            // push old first max to new second max
            MaxCountSoFar_2 = MaxCountSoFar_1;
            MaxIndexSoFar_2 = MaxIndexSoFar_1;
            // new first max
            MaxCountSoFar_1 = CountThatOne;
            MaxIndexSoFar_1 = i;
        } else {
            // catch second one, but not if equal to first
            if ( CountThatOne > MaxCountSoFar_2 && szInput[ i ] != szInput[ MaxIndexSoFar_1 ] ) {
                MaxCountSoFar_2 = CountThatOne;
                MaxIndexSoFar_2 = i;
            }
        }
    }
    if ( MaxIndexSoFar_1 >= 0 ) {
        printf( "Most seen char is %c, first seen at index %d\n", szInput[ MaxIndexSoFar_1 ], MaxIndexSoFar_1 );
        if ( MaxIndexSoFar_2 >= 0 ) {
            printf( "Second Most seen char is %c, first seen at index %d\n", szInput[ MaxIndexSoFar_2 ], MaxIndexSoFar_2 );
        }
    }
    return 0;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2021-06-15
    相关资源
    最近更新 更多