【问题标题】:Unable to execute a code that finds first character of a string that matches with a given character无法执行找到与给定字符匹配的字符串的第一个字符的代码
【发布时间】:2019-08-10 13:32:30
【问题描述】:

编写此代码是为了从后面识别字符串中首先与给定字符匹配的字符位置。当我使用 scanf 获取字符串时,编译器不要求输入字符并直接将输出作为 0 .我无法纠正 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 -1;。它会在第一次迭代后导致函数返回。
  • 使 n=strlen(str)-1; 成为 str[strlen(str)]\0(虽然没有害处)。
  • @PaulOgilvie 实际上 '\0 是字符串的有效字符。至少 strchr 返回指向 '\0' 的指针。

标签: c char character c-strings strrchr


【解决方案1】:

你包含了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 );

并在找不到字符的情况下返回( size_t )-1,因为标准C函数strlen的返回类型是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] == '\0' ? '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] == '\0' ? '0' : s[n] );
        }
        else
        {
            printf( "The character %c is found at position %zu\n", s[n] == '\0' ? '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会读取一个字符串,直到遇到一个空白字符。

所以不要使用scanf,而是使用fgets。例如

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

【讨论】:

  • 我明白你想要传达的意思,但是当我使用 scanf 时,它仍然不会读取字符。
猜你喜欢
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 2019-11-13
相关资源
最近更新 更多