无法执行查找与给定字符匹配的字符串的第一个字符的代码

Unable to execute a code that finds first character of a string that matches with a given character

编写这段代码是为了从后面识别字符在字符串中的位置,首先匹配给定的 character.When 我使用 scanf 获取字符串,编译器不要求字符并直接给出0.I 的输出无法用 scanf 纠正问题。

我 运行 通过不使用 scanf 直接提供字符串输入的函数,它工作正常。

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

int strrindex(char str[], char t)  
{   
int n=strlen(str);  

    while(n>=0)  
    {  
        if(str[n]==t)  
        {  
        return n;  
        }  
        else  
        {  
            n=n-1;  
        }       
    }
    return -1;
}  

int main()  
{  
    int k;  

    char str[100];  

    printf("enter line\n");  

    scanf("%s",str);  

    char t;  

    printf("enter letter\n");  

    scanf(" %c",&t);  

    k=strrindex(str,t);  

    int p=k+1;  

        printf("the position is %d",p);  
}  

代码运行但输出始终为 0,主要是因为 scanf 添加了 \n。

您包含了 return 声明

return -1;  

在 while 循环中

while(n>=0)  
{  
    if(str[n]==t)  
    {  
    return n;  
    }  
    else  
    {  
        n=n-1;  
    }     
return -1;  
}

将它放在循环之外。

注意函数要这样声明

size_t strrindex( const char str[], char t );

和return ( size_t )-1在找不到字符的情况下,因为标准C函数strlen的return类型是size_t

请记住,有一个类似的标准 C 函数

char *strrchr(const char *s, int c);

这是一个演示程序

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

size_t strrindex( const char *s, char c )
{
    size_t n = strlen( s );

    while ( s[n] != c && n != 0 ) --n;

    return n == 9 ? -1 : n;
}


int main(void) 
{
    const char *s = "Hello";

    size_t n = strlen( s );

    do
    {
        size_t pos = strrindex( s, s[n] );

        if ( pos == -1 ) 
        {
            printf( "The character %c is not found\n", s[n] );
        }
        else
        {
            printf( "The character %c is found at position %zu\n", s[n] == '[=14=]' ? '0' : s[n], pos );
        }
    } while ( n-- );

    return 0;
}

它的输出是

The character 0 is found at position 5
The character o is found at position 4
The character l is found at position 3
The character l is found at position 3
The character e is found at position 1
The character H is found at position 0

如果您想从搜索中排除终止零,则该函数可以按以下方式查看

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

size_t strrindex( const char *s, char c )
{
    size_t n = strlen( s );

    while ( n != 0 && s[n - 1] != c ) --n;

    return n == 0 ? -1 : n - 1;
}


int main(void) 
{
    const char *s = "Hello";

    size_t n = strlen( s );

    do
    {
        size_t pos = strrindex( s, s[n] );

        if ( pos == -1 ) 
        {
            printf( "The character %c is not found\n", s[n] == '[=16=]' ? '0' : s[n] );
        }
        else
        {
            printf( "The character %c is found at position %zu\n", s[n] == '[=16=]' ? '0' : s[n], pos );
        }
    } while ( n-- );

    return 0;
}

在这种情况下,程序输出是

The character 0 is not found
The character o is found at position 4
The character l is found at position 3
The character l is found at position 3
The character e is found at position 1
The character H is found at position 0

还要注意函数scanf读取一个字符串,直到遇到一个white-space字符。

因此请使用 fgets 而不是 scanf。例如

fgets( str, sizeof( str ), stdin );
str[strcspn( str, "\n" )] = '[=18=]';