简体   繁体   English

将字符与 C 中的字符串进行比较

[英]comparing a char to a string in C

I'm quite new to C and I'm wondering why in the code below, the char I'm comparing to each letter of the string word is showing that it's equal everytime.我对 C 很陌生,我想知道为什么在下面的代码中,我与字符串单词的每个字母进行比较的字符显示每次都相等。 For example例如

If I've inputted the word如果我输入了这个词

apple苹果

and I'm looking for any repeating char in "apple" my function.我正在“苹果”我的函数中寻找任何重复的字符。 I pass in to the function each char of apple such as a, p, p etc. It should return 1 when I pass in p since it's repeated, but instead, for every char of apple, my function says a == word[0], a == word[1] even though word[1] for "apple" is 'p'.我将苹果的每个字符传递给函数,例如 a、p、p 等。当我传入 p 时它应该返回 1,因为它是重复的,但是,对于苹果的每个字符,我的函数说 a == word[0 ], a == word[1] 即使“apple”的 word[1] 是 'p'。 I know char is ASCII, so each char has a number value, but I'm not sure why this is not working.我知道 char 是 ASCII,所以每个 char 都有一个数值,但我不确定为什么这不起作用。 Perhaps, I'm using the pointer *word in the functions arguments incorrectly?也许,我在函数参数中错误地使用了指针 *word?

My code is below for my function, rpt_letter:下面是我的函数 rpt_letter 的代码:


int rpt_letter(char *word, char c)
{

    int i;
    int count = 0;
    i = 0;

    printf("This is the WORD %s\n", word);


    while(count < 2)
    {
        if(word[i] == c)
        {
            count++;
            printf("the count is %d\n the char is %c and the string is %c\n", count, c, word[i]);
        }
        i++;
    }

    if (count<2) 
    {
       // printf("letter %c was not found in the array. \n", c);
        return 0;
    } 
    else
    {
        //printf("letter %c was found at index %d in the array.\n", c, mid);
        repeats[rpt_counter] = c;
        rpt_counter++;
        return 1;
    }

    return 0;
}

I'll include the main method just in case -- but I believe the main method is working well以防万一,我将包含主要方法 - 但我相信主要方法运行良好

int main(void) 
{
    //! showArray(list, cursors=[ia, ib, mid])
    //int n = 51;
    char word[51]; 
    scanf("%s", word);

    //length of string
    for (n=0; word[n] != '\0'; n++); //calculate length of String
    printf("Length of the string: %i\n", n);

    int count = 0;

    //sort words

    int i;
    char swap = ' '; 

    for(int k = 0; k < n; k++)
    {
    for (i=0; i<n-1; i++)
    {

        //if prev char bigger then next char
        if (word[i] > word[i+1]) 
        {

            //make swap = prev char
            swap = word[i];

            //switch prev char  with next char
            word[i] = word[i+1];

            //make next letter char
            word[i+1] = swap;
        }
    }
    }
    printf("%s\n", word);

    for (i=0; i<n-1; i++) 
    {
     int rpt = rpt_letter(word, word[i]);  
     if(rpt == 1)
     {
         count++;
     }
    }

    printf("%d", count);

 return 0;   
}

I've tried a number of things such as using the operator !=, also <, > but it gives me the same result that each word[ia] == c.我尝试了很多方法,例如使用运算符 !=,还有 <,>,但它给了我相同的结果,每个单词 [ia] == c。

You are getting this issue because in your code rpt_letter() the while loop has a terminating condition count >= 2 .您遇到此问题是因为在您的代码rpt_letter() ,while 循环具有终止条件count >= 2 Now consider input apple and character a .现在考虑输入apple和字符a As a appears in apple only once, the count after traversing the whole word remains 1. But the loop doesn't terminate.由于aapple只出现一次,遍历整个单词后的计数仍为 1。但循环不会终止。 So, the index i becomes greater than the length of string and tries to check the character appearing after that.因此,索引i变得大于字符串的长度并尝试检查之后出现的字符。

The loop terminates eventually when it gets another a this way.当它a这种方式获得另一个a时,循环最终终止。 You need to add a check for the terminating null character in your loop so that it doesn't cross the length of the string .您需要在循环中添加对终止空字符的检查,以便它不会超过 string 的长度。

Change the while loop condition to something like - while((count < 2) && (word[i] != '\\0'))将 while 循环条件更改为类似 - while((count < 2) && (word[i] != '\\0'))

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

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