How to find the last occurrence of character in string with C

2 Answers

0 votes
#include <stdio.h> 
#include <string.h> 

int main(void)
{   
    char s[30] = "c c++ c# java python";
    char *p;
    
    p = strrchr(s, ' ');
    
    printf("%s", p);
     
    return 0;
}
  
        
/*
run:
     
 python

*/

 



answered Feb 24, 2017 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main(void) 
{
    char s[] = "c c++ c java";
    char ch = 'a';

    {
        char *last = strrchr(s, ch);
        if (last != NULL) 
        {
            printf("Last position is: %s\n", last);
            printf("Last position is: %ld\n", last - s);
        }
        else
        {
            printf("Not Found");
        }
    }
    
    return 0;
}
   
/*
run:

Last position is: a
Last position is: 11

*/

 



answered Aug 6, 2017 by avibootz
...