+ 1

By C function How to check if string mirrored or not?

I tried this but didn't work: #include <stdio.h> #include <stdlib.h> void check_string_mirrored(char *str, int size ) { int i; for(i=0; i<size; i++) { if(str[i] == str[size-i]) { printf("string is mirrored\n"); } else printf("string is not mirrored\n"); } } int main() { char s[5]= "ARBRA"; check_string_mirrored(s,5); return 0; }

14th Oct 2018, 12:58 PM
Aisha Ali
Aisha Ali - avatar
1 Réponse
0
You need to compare str[i] with str[size-i-1]. #include <stdio.h> #include <stdlib.h> void check_string_mirrored(char *str, int size ) { int i; int mirrored = 1; for(i=0; i<size; i++) { if(str[i] != str[size-i-1]) { mirrored = 0; break; } } printf("string is"); printf(mirrored ? "" : " not"); printf(" mirrored\n"); } int main() { char s[5]= "ARBRA"; check_string_mirrored(s,5); return 0; }
14th Oct 2018, 1:14 PM
Anna
Anna - avatar