Quick Google Search

String Function strlen( ), strcpy( ), strcat( ) & strcmp( ).


In C, we have important string functions: strlen( ), strcpy( ), strcat( ) &
strcmp( ). If you know the efficient coding of these functions, it will certainly help you to
mprove your programming skills. All these functions are coded with WAR coding style.
8.1 strlen( )
int strlen( char *s )
 {
  char *ptr = s;
  while( *ptr++ )
   ;
  return( ptr-s );
 }
8.2 strcpy( )
char *strcpy( char *s, char *t )
 {
  char *ptr=s;
  while( *s++ = *t++ )
   ;
  return( ptr );
 }
8.3 strcat( )
char *strcat( char *s, char *t )
 {
  char *ptr=s;
  while( *s++ )
   ;
  while( *s++ = *t++ )
   ;
  return( ptr );
 }
8.4 strcmp( )
int strcmp( char *s, char *t )
 {
        int n;
       while ((n = *s - *t++) == 0 && *s++)
                 ;
       return( n );
 }

No comments:

Popular Posts