How to find the last character that is repeated in a string with C

1 Answer

0 votes
#include <stdio.h> 
#include <string.h> 
   
int get_last_character_repeated(char* s) { 
    int ch_idx = -1;
     
    for (int i = 0; i < s[i]; i++) { 
        for (int j = i + 1; j < s[i]; j++) { 
            if (s[i] == s[j]) { 
                ch_idx = i;
                break;
            } 
        }
    } 
    return ch_idx; 
}  
int main() {
    char s[] = "abcdxypcbadom"; 
    int i = get_last_character_repeated(s); 
     
    if (i == -1) 
        printf("Not found\n"); 
    else
        printf("%c\n", s[i]); 
         
    return 0;
}
 
 
 
/*
 
d
 
*/

 



answered Jan 24, 2020 by avibootz
edited Jan 24, 2020 by avibootz

Related questions

...