C Program to Find Last Occurrence of a Character in a String

C Program to Find Last Occurrence of a Character in a String

C program to find the last occurrences of a character in a string; Through this tutorial, we will learn how to find the last occurrences of a character in a string using for loop, while loop, recursion, and functions in c programs.

Programs to Find Last Occurrence of a Character in a String in C

  • C Program to Find Last Occurrence of a Character in a String using For Loop
  • C Program to Find Last Occurrence of a Character in a String using While Loop
  • C Program to Find Last Occurrence of a Character in a String Using Recursion
  • C Program to Find Last Occurrence of a Character in a String using Function

C Program to Find Last Occurrence of a Character in a String using For Loop

#include <stdio.h>
#include <string.h>
 
int main()
{
    char s[1000],c;  
    int i,n,k=0;
 
    printf("Enter  the string : ");
    gets(s);
    printf("Enter character to be searched: ");
    c=getchar();
    
    for(i=strlen(s)-1;i>=0;i--)  
    {
    	if(s[i]==c)
    	{
		  k=1;
    	  break;
		}
 	}
    if(k)
 	    printf("character  %c  is last occurrence at location:%d ",c,i);
    else
        printf("character is not in the string ");
 
 	 
     
    return 0;
}

The Output of the above c program; as follows:

Enter  the string : hello world
Enter character to be searched: o
character  o  is last occurrence at location:7 

C Program to Find Last Occurrence of a Character in a String using While Loop

/* C Program to find Last Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, index;
  	i = 0;
  	index = -1;
 
  	printf("\n Please Enter any String : ");
  	gets(str);
  	
   printf("Enter character to be searched : ");
   
  	scanf("%c", &ch);
  	
  	while(str[i] != '
/* C Program to find Last Occurrence of a Character in a String */
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, index;
i = 0;
index = -1;
printf("\n Please Enter any String : ");
gets(str);
printf("Enter character to be searched : ");
scanf("%c", &ch);
while(str[i] != '\0')
{
if(str[i] == ch)  
{
index = i;	
}
i++;
}
if(index == -1)
{
printf("character is not in the string ");
}
else
{
printf("character  %c  is last occurrence at location:%d ",ch,index);
}	
return 0;
}
') { if(str[i] == ch) { index = i; } i++; } if(index == -1) { printf("character is not in the string "); } else { printf("character %c is last occurrence at location:%d ",ch,index); } return 0; }

The Output of the above c program; as follows:

Please Enter any String : hello world
Enter character to be searched : o
character  o  is last occurrence at location:7 

C Program to Find Last Occurrence of a Character in a String Using Recursion

#include <stdio.h>
 #include <string.h>
 
 
int check(char *s,char c)
{
     int i=strlen(s)-1;
    if(i<0)
    {
        return i;
	}
	else
	{
		 if(s[i]==c)
		 return i;
		 i--;
		 check(s,c);
	}
 
    
 	
 	 
 	
}
int main()
{
    char s[1000],c;  
    int n;
 
    printf("Enter  the string : ");
    gets(s);
    printf("Enter character to be searched: ");
    c=getchar();
    n=check(s,c);
 
     
    if(n>-1)
 	    printf("character  %c  is last occurrence at location:%d ",c,n);
    else
        printf("character is not in the string ");
        return 0;
 }

The Output of the above c program; as follows:

Enter  the string : hello programmer
Enter character to be searched: r
character  r  is last occurrence at location:15 

C Program to Find Last Occurrence of a Character in a String using Function

#include <string.h>
 
int check(char *s,char c)
{
    int i,k=0;
    for(i=strlen(s)-1;i>=0;i--)  
    {
    	if(s[i]==c)
    	{
		  k=1;
    	  break;
		}
 	}
   return i;
 	
	  
 }
int main()
{
 
    char s[1000],c;  
    int n;
 
    printf("Enter  the string : ");
    gets(s);
    printf("Enter character to be searched: ");
    c=getchar();
    n=check(s,c);
 
     
    if(n>-1)
 	    printf("character  %c  is last occurrence at location:%d ",c,n);
    else
        printf("character is not in the string ");
 
 	 
     
    return 0;
 
     
     
}

The Output of the above c program; as follows:

Enter  the string : welcome to c programming
Enter character to be searched: p
character  p  is last occurrence at location:13 

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published.