1

This code is written to identify the position of character in the string from back which matches first with a given character.When i use scanf to get string,the compiler doesn't ask for the character and directly gives the output as 0.I am unable to rectify the problem with scanf.

I ran the function by giving string input directly without scanf and it works fine.

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

The code runs but the output is always 0 mostly because of \n added because of scanf.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

1 Answers1

0

You included the return statement

return -1;  

in the while loop

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

Place it outside the loop.

Pay attention to that the function should be declared like

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

and return ( size_t )-1 in the case when the character is not found because the return type of the standard C function strlen is size_t.

Bear in mind that there is a similar standard C function

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

Here is a demonstrative program

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

Its output is

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

If you want to exclude the terminating zero from searching then the function can look the following way

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

In this case the program output is

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

Also pay attention to that the function scanf reads a string until a white-space character is encountered.

So instead of scanf use fgets. For example

fgets( str, sizeof( str ), stdin );
str[strcspn( str, "\n" )] = '\0';
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335