Home / C Programming / Strings :: Discussion

Discussion :: Strings

  1. The library function used to find the last occurrence of a character in a string is

  2. A.

    strnstr()

    B.

    laststr()

    C.

    strrchr()

    D.

    strstr()

    View Answer

    Workspace

    Answer : Option C

    Explanation :

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

    It scans a string s in the reverse direction, looking for a specific character c.

    Example:

     

    #include  
    #include  
    
     int main(void) 
     {  
          char text[] = "I learn through FresherGate.com";  
          char *ptr, c = 'i';    
    
         ptr = strrchr(text, c);    
         if(ptr)     
            printf("The position of '%c' is: %d\n", c, ptr-text); 
         else    
            printf("The character was not found\n");  
          return 0; 
     }
    

    Output:

    The position of 'i' is: 19


Be The First To Comment