如何巧妙的在字符串末尾添加'\0'

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
int
prepare_key( char *key )
{
    register char *keyp;
	register char *dup;
    register int character;

	if( *key == '\0' )
      return FALSE;

	for( keyp = key; ( character = *keyp ) != '\0'; keyp++ )
		{
           if( !islower( character ) )
			   {
                   if( !isupper( character ) )
				   {
                      return FALSE;
				   }
                   *keyp = tolower( character );
               }
        }
   for( keyp = key; ( character = *keyp ) != '\0'; )
	   {
           dup = ++keyp;
           while( ( dup = strchr( dup, character ) ) != NULL )
                 strcpy( dup, dup + 1 );
       }
	  for( character = 'a'; character <= 'z'; character++ )
		  {
              if( strchr( key, character ) == NULL )
				  {
                       *keyp++ = character;
                        *keyp = '\0';//trailbzescdfghjkmnopquvwxy在最后的位置加上'\0'
                  }
          }
		  printf( "%s\n", key);
}


int main()
{
    char a[] = "TRAILBAZERS";
    prepare_key( a );
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sanpam/article/details/82779425