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

1 Answer

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

 



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

Related questions

...