简体   繁体   English

如何找到数组中第k个字符出现的位置?

[英]How can I find the the position of the k-th occurrence of a character in an array?

I'm writing a program which takes a string, a single character and an integer number k , then it finds the k-th occurence of given character in given string. 我正在编写一个包含字符串,单个字符和整数k的程序 ,然后在给定字符串中找到给定字符的第k次出现。

I think the code is correct but the code below doesn't give proper results. 我认为代码是正确的,但是下面的代码没有给出正确的结果。

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;
}

There are a couple of mistakes in func() : func()中有几个错误:

Look at: 看着:

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

Either use a normal while, or write i=strlen(ch); 要么使用正常的时间,要么写i=strlen(ch);

Then, when you write this you count all the occurences: 然后,在编写此代码时,您要计算所有发生的次数:

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++;
}

For example, if there are two 'e' in ch[] and you look for the first occurence, when the loop is over, there will be 2 in c, so it will fail ! 例如,如果ch []中有两个'e',并且您寻找第一个匹配项,则当循环结束时,c中将有2,因此它将失败!

Rewrite this loop: 重写此循环:

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++;             
}

But there is also a display issue in you main programme. 但是主程序中也存在显示问题。 Look at: 看着:

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

Ok he'll print that he found something, but he will always print n, because (ch, character, n) is not a function call but the comma operator which returns the most right element. 好的,他将打印出他发现了一些东西,但是他将始终打印n,因为(ch,character,n)不是函数调用,而是返回最右边元素的逗号运算符。

Rewrite this as: 重写为:

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

Well, you have many problems inside the code, but the most relevant part is this one: 好吧,您在代码内部有很多问题,但是最相关的部分是这一部分:

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

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

    j++;
}

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

You count the number of occurrences in the whole string, and then check if you were lucky enough to have the exact number of occurrences needed inside the whole string. 您对整个字符串中的出现次数进行计数,然后检查您是否幸运地拥有整个字符串中所需的确切出现次数。

What you actually want to do, is to check if you found n-th occurrence inside the loop, like this: 您真正想要做的是检查是否在循环内发现了第n个出现,例如:

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

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

Then you probably want to read up on strlen , strchr , and fix other bugs in your code. 然后,您可能想要阅读strlenstrchr并修复代码中的其他错误。

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

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