i've following char arrays:
filename[256] = "one.txt"; shortname[11] = "one txt"; what best way compare these 2 arrays in c?
the first 8 characters of short name must fit characters before . in filename.
i case situation above valid.
i not know best way compare strings straightforward approach can following way
#include <stdio.h> #include <ctype.h> int main( void ) { char filename[256] = "one.txt"; char shortname[11] = "one txt"; char *p = filename; char *q = shortname; size_t = 0; while ( < 8 && ( char )toupper( ( unsigned char )*p ) == *q ) ++i, ++p, ++q; if ( *q == ' ' || == 8 ) puts( "they coincide" ); } the program output is
they coincide that program checks whether initial characters of filename coincide shortname. there can more characters before point in filname there characters in shortname. otherwise can add condition *p == '.'
Comments
Post a Comment