通过递增指针来搜索数组中的字符?

时间:2017-04-01 20:29:10

标签: c arrays string pointers

所以我声明的功能似乎没有按预期工作,即便如此,我也不认为通过递增指针来比较字符的正确方法,所以我一般都在这里迷路了。说实话,指针一直困扰着我,如果我要在C上变得更好,我真的需要学习如何使用它们。感谢任何人的帮助!

这里是我的代码,如果这有帮助,程序的目的是让你输入一行文字,输入一个字符来搜索那行文字,然后找到那些字符使用CharIsAt。 (稍后将添加以下内容)存储在"找到的值#34;然后也会打印出来。

#include <stdio.h>
#define SIZE 41
int CharIsAt(char *pStr,char ch,int loc[],int mLoc);
int main(void){
    char array[SIZE],search;
    int found[SIZE],chars;
    printf("Enter a line of text(empty line to quit): ");
    while (fgets(array,SIZE, stdin)!=NULL && array[0]!='\n')
    {
        printf("Enter a character to search: ");
        search=getchar();
        chars=CharIsAt(array,search,found,SIZE);
    }
    return 0;
}
int CharIsAt(char *pStr,char ch,int loc[],int mLoc){
    //Searches for ch in *pStr by incrementing a pointer to access
    //and compare each character in *pStr to ch.
    int i,x;
    for (i=0;i<mLoc;i++){
        if (strcmp(pStr[i],ch)==0){
            //Stores index of ch's location to loc
            loc[i]=pStr[i];
            x++;
        }
    }
    //Returns the number of times ch was found
    return x;
}

编辑:在for循环中翻转符号。现在该计划给了我一个“停止工作”#34;错误。

0 个答案:

没有答案