Find last occurrence of a character in the string

C program to find last occurrence of a character in the string.

Program

#include<stdio.h>
#include<conio.h>
#define MAX 50

void main()
{
	char str[MAX];
    int i, flag, pos;
    char ch;

    printf("Enter the string\t:");
    gets(str);
    printf("Enter the character to be searched\t:");
    scanf("%c",&ch);

    i = 0;
    flag = 0;
    while(str[i] != '\0')
    {
        if(str[i] == ch)
        {
            pos = i;
            flag = 1;
        }
        i++;
    }
    if(flag == 0)
    {
        printf("\nCharacter not found");
    }
    if(flag == 1)
    {
        printf("\nLast occurrence of the character '%c' is found at position %d",ch,pos+1);
    }

    getch();
}

Output

Enter the string        :welcome to coursecrux.com
Enter the character to be searched      :c

Last occurrence of the character 'c' is found at position 23