User:Atlant/String symmetry.c
Appearance
#include <stdio.h> #include <string.h> /* Forward routines * */ int is_symmetrical( char string[] ); int main( int argc, char * argv[], char * envp[] ); void try_one( char string[] );
int main( int argc, char * argv[], char * envp[] ) { try_one( "" ); try_one( "A" ); try_one( "AB" ); try_one( "AA" ); try_one( "ABC" ); try_one( "ABA" ); try_one( "ABCD" ); try_one( "ABBA" ); try_one( "ABBC" ); try_one( "ABCA" ); return( 0 ); }
void try_one( char string[] ) { int status; status = is_symmetrical( string ); if (status) { printf( "\"%s\" is symmetrical\n", string ); } else { printf( "\"%s\" isn't symmetrical\n", string ); } }
int is_symmetrical( char string[] ) { int i; int j; i = 0; // Position at first byte j = strlen(string)-1; // Position at last byte while (i<j) // Until indices cross or coincide... { if (string[i] != string[j]) return( 0 ); i++; j--; } return( 1 ); }