【问题标题】:How can I find the the position of the k-th occurrence of a character in an array?如何找到一个字符在数组中第 k 次出现的位置?
【发布时间】:2014-08-13 14:59:12
【问题描述】:

我正在编写一个程序,它接受一个字符串、一个字符和一个整数k,然后它会找到给定字符串中给定字符的第 k 次出现。

我认为代码是正确的,但下面的代码没有给出正确的结果。

char ch[100];
char character;
int n;
cout << endl << "Type a string of characters : ";
gets_s(ch);
cout << endl << "Enter a single Character : ";
cin >> character;
cout << "Enter the event : ";
cin >> n;
if ( func_1( ch,  character,  n)!=-1)
    cout << endl << n << "th event is in the position number  " << (ch, character, n)<< endl;
else
    cout << "Event couldn't be found  ." << endl;
}

 int func_1(char ch[], char character, int n)
 {
    int i = 0;
    int c = 0;

    do
    {
       i++;
    } while (ch[i] != '\0');

    int j;

    for (j = 0; j < i-1;)
    {

        if (ch[j] == character)
            c = c + 1;

        j++;
    }

    if (c == n)
        return j-1;

    else
        return -1;
}

【问题讨论】:

  • 你是什么意思:“这个整数表示该单个字符发生在数组块中的事件”?我完全不知道这应该是什么意思。
  • @JerryCoffin 例如,您的字符串是:Jerry Coffin 您的单个字符是:f 您的整数是:2 结果必须是第二个“f”位于数组的第 10 个块中。
  • 也许我今天早上速度很慢,但这似乎并不能说明什么。
  • 你要的词不是“事件”,而是“发生”,可能会让一些人头疼:)
  • 所以,给定一个字符串 s,一个字符 c 和一个整数 k,你想找到索引k: c 在s 中的第一次出现?如果我是你,我会首先解决找到第一个出现的问题。然后考虑如何循环该解决方案。

标签: c++ arrays string


【解决方案1】:

好吧,你的代码里面有很多问题,但最相关的部分是这个:

for (j = 0; j < i-1;)
{

    if (ch[j] == character)
        c = c + 1;

    j++;
}

if (c == n)
    return j-1;
else
    return -1;

您计算整个字符串中出现的次数,然后检查您是否有幸在整个字符串中获得所需的确切出现次数。

您真正想要做的是检查您是否在循环中发现了 n-th 项,如下所示:

for (j = 0; j < i-1;)
{

    if (ch[j] == character)
        c = c + 1;
    if (c == n)
        return j;  
    j++;
}

那么您可能想要阅读strlen、strchr,并修复代码中的其他错误。

【讨论】:

    【解决方案2】:

    func()中有几个错误:

    看:

    do {
        i++;
    } while (ch[i] != '\0');   // ouch will loop forever if ch[] is "" !! 
    

    要么使用普通的while,要么写i=strlen(ch);

    然后,当你写这个时,你会计算所有出现的次数:

    for (j = 0; j < i - 1;) {   // loop unil end of string  
        if (ch[j] == character)
            c = c + 1;            // But if there are several occurences, he will count them all 
        j++;
    }
    

    例如,如果 ch[] 中有两个 'e' 并且你寻找第一个出现,当循环结束时,c 中有 2,所以它会失败!

    重写这个循环:

    for (j = 0; j < i - 1 && c!=n;j++) {   // loop unil end of string => put j++ here for clarity, and exit if you've fount what you've searched
        if (ch[j] == character)
            c++;             
    }
    

    但是您的主程序中也存在显示问题。看:

    if (func_1(ch, character, n) != -1)
        cout << endl << n << "th event is in the position number  " << (ch, character, n) << endl;
    

    好吧,他会打印出他找到的东西,但他总是会打印 n,因为 (ch, character, n) 不是函数调用,而是返回最右边元素的逗号运算符。

    改写为:

    int result = func_1(ch, character, n);
    if (result != -1)
        cout << endl << n << "th event is in the position number  " << result << endl;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 2019-07-13
      • 2020-05-26
      • 1970-01-01
      相关资源
      最近更新 更多