C Program to find last occurrence of character in string

list of programs below: C Program to find last occurrence of character in string using functions, C Program to find last occurrence of character in string without functions

C Program to find last occurrence of character in string using functions
C Program to find last occurrence of character in string using functions

To write a c program to find last occurrence of character in string, we need inputs a  string and a  character . Then calculate string length. Starting from last character in string to first character of string, seach for occurrence of input character and decrement count variable when character not found. if character is found, output count which is position of character and exit.

write a C Program to find position of last occurrence of character in string using functions

write a C Program to find last occurrence of a character in a string using functions

#include<stdio.h>
main()
{
char str[30];
char c
printf(“\n enter the string \n”);
gets(str);
printf(“\n enter the character \n”);
gets(c);
printf( “\n last occurrence of character is at position no. %d”, find(str,c));
}

int find( const char list[], char what )
{
int i,=0 count=0 ;
while (list[i] != “\0”)
{
i++
}
while(i>0)
{
if (strCmp(list[i],what )==0)
return (i+1 );
}
}

OUTPUT:

enter the string
Mydream
enter the character
m
last occurrence of character is at position no. 7

write a C Program to find last occurrence of character in string using pointers and functions

write a C Program to find last occurrence of a character in a string using pointers

#include<stdio.h>
main()
{
string *str;
char c
printf(“enter the string\n”);
gets(str);
printf(“\n enter the character \n”);
gets(c);
Last_occurrence(str,c);
}

void Last_occurrence(string str, char c)
{
int count=0;
while (*str!=’\0′)
{
*str++;
count++;
}
while (count>0)
{
if (strcmp(*str,c )==0)
{
printf( ” \n last occurrence of character is at position no. %d”, count+1)
exit(0);
}
*str–;
count–;
}
printf( “\n The character does not exist in string”)
}

OUTPUT:

enter the string
Hello
enter the character
l
last occurance of occurrence is at position no. 4

write a C Program to find last occurrence of character in string using arrays

write a C Program to find last occurrence of character in string using arrays

#include<stdio.h>
main()
{
char str[30];
char c
printf(“\n enter the string \n”);
gets(str);
printf(“\n enter the character \n “);
gets(c);
int i=0;
while str[i]!=’\0′)
{
i++ ;
{
while (i>0)
{
if (strCmp(str[i],c)==0)
printf( “\n last occurrence of character is at position no. %d”, i+1)
exit(0);
}
i–;
}
printf( “\n The character does not exist in string”)
}

OUTPUT:

enter the string
programmingsimplysolved
enter the character
j
The character does not exist in string

write a C Program to find last occurrence of character in string using pointers

write a C Program to find last occurrence of a character in a string using pointers

#include<stdio.h>
main()
{
string *str;
char c
printf(“enter the string\n”);
gets(str);
printf(“\n enter the character \n”);
gets(c);
int count=0;
while (*str!=’\0′)
{
*str++;
count++;
}
while (count>0)
{
if (strCmp(*str,c )==0)
{
printf( ” \n last occurrence of character is at position no. %d”, count+1)
exit(0);
}
*str–;
count–;
}
printf( “\n The character does not exist in string”)
}

OUTPUT:

enter the string
Hello
enter the character
l
last occurance of occurrence is at position no. 4

Other programs:

 

Other programs:

Static variable scope
Register variable scope
External variable scope
Automatic variable scope
amicable number
Prime number
Perfect number
Palindrome number
Strong number
Find Factorial 
Biggest Number
Average of n numbers
convert decimal number to octal
decimal number to hexadecimal
decimal number to binary
simple interest
compound interest
Add sparce matrices
Add matrices
 file copying
Palindrome string
Reverse string
String length
Replace characters in string
substring or not using arrays
First occurrence of character in string
Last occurrence of character in string
Convert to uppercase
Convert to lowercase
Concatenate strings 
Compare strings
Generate Employee payslips
Count students scoring marks>50%
convert weight into grams

Leave a Reply

Your email address will not be published. Required fields are marked *