字符串函数(三)

字符串函数(三)

1.strncmp ()函数
strncmp ()函数可以比较到字符串的不同处,也可以比较完由第三个参数指定的字符串。
例如,如果想搜索以”astro”开头的字符串,您可以限定搜索前5个字符。

//使用strncmp ()函数
#include < stdio.h >
#inlcude < string . h >
 
#define LISTSIZE 5
 
int main ( void )
{
    
char * list [ LISTSIZE ] = {
                
" astronomy " , " astounding " ,
                
" astrophysics " , " ostracize " ,
                
" asterism "    
                
} ;
    
int count = 0 ;
    
int i ;
    
    
for ( i = 0 ; i < LISTSIZE ; i ++ )
    
{
        
if ( strncmp ( list [ i ] , " astro " , 5 ) == 0 )
        
{
            
printf ( " Found: %s \ n " , list [ i ]) ;
            
count ++;
        
}
    
}
    
    
printf ( " The list contained %d words beginning with astro. \ n " , count ) ;
    
    
return 0 ;
}

2.strcpy () 和strncpy ()函数
如果pts1和pts2都是指向字符串的指针,则下面的表达式只复制字符串的地址而不是字符串本身。

pts1 = pts2 ;

如果你确实希望复制字符串,那么可以使用strcpy ()函数。

//copy1.c strcpy ()示例
#include < stdio.h >
#include < string.h > //声明strcpy ()函数
#define SIZE 40
#define LIM 5
 
int main ( void )
{
    
char qwords [ LIM ][ SIZE ] ;
    
char temp [ SIZE ] ;
    
int i = 0 ;
 
    
printf ( " Enter %d words beginning with q: \ n " , LIM ) ;
    
while ( i < LIM && gets ( temp ))
    
{
        
if ( temp [ 0 ] != ' q ' )
        
{
            
printf ( " %s doesn't begin with q! \ n " , temp ) ;
        
}
        
else
        
{
            
strcpy ( qwords [ i ] , temp ) ;
            
i ++;
        
}        
    
}
    
puts ( " Here are the words accepted: " ) ;
    
for ( i = 0 ; i < LIM ; i ++ )
    
{
        
puts ( qwords [ i ]) ;
    
}
 
    
return 0 ;
}

请注意只有当输入的单词通过了q判断,计数值i才会增加。
这里使用了一个基于字符的判断

if ( temp [ 0 ] != ' q ' )

这相当于

if ( strncmp ( temp , " q " , 1 ) != 0 )

注意在,strcpy 两个参数中,第二个参数temp指向的字符串被复制到第一个参数qword[i]所指向的数组中。
temp被称为源字符串,qword被称为目标字符串。
为什么要这么设计,为什么不设计到前面?
如果注意到它和赋值语句的顺序一样,目标在左边,就很容易记住参数的顺序。

3.strcpy ()的其它特性
首先,它是一个char *类型,它返回的是第一个参数的值。
第二,第一个参数不需要指向数组的开始,这样就可以只复制数组的一部分。

//copy2.c
#include < stdio.h >
#include < string.h >
#define WORDS " beast "
#define SIZE 40
 
int main ( void )
{
    
char * orig = WORDS ;
    
char copy [ SIZE ] = " Be the best that you can be. " ;
    
char * ps ;
 
    
puts ( orig ) ;
    
puts ( copy ) ;
    
ps = strcpy ( copy + 7 , orig ) ;
    
puts ( copy ) ;
    
puts ( ps ) ;
 
    
return 0 ;
}

注意ps指向copy的第8个元素,这是因为第一个参数是copy + 7。

4. strncpy ()
strcpy ()的缺点显而易见,它不检查目标字符串是否能容纳得下源字符串。
strncpy ()会用第三个参数指明最大可复制的字符数。

//copy3.c strncpy ()示例
#include < stdio.h >
#include < string.h > //声明strcpy ()函数
#define SIZE 40
#define TARGSIZE 7
#define LIM 5
 
int main ( void )
{
    
char qwords [ LIM ][ TARGSIZE ] ;
    
char temp [ SIZE ] ;
    
int i = 0 ;
 
    
printf ( " Enter %d words beginning with q: \ n " , LIM ) ;
    
while ( i < LIM && gets ( temp ))
    
{
        
if ( temp [ 0 ] != ' q ' )
        
{
            
printf ( " %s doesn't begin with q! \ n " , temp ) ;
        
}
        
else
        
{
            
strncpy ( qwords [ i ] , temp , TARGSIZE - 1 ) ;
            
qwords [ i ][ TARGSIZE - 1 ] = '\ 0 ';
            
i ++;
        
}        
    
}
    
puts ( " Here are the words accepted: " ) ;
    
for ( i = 0 ; i < LIM ; i ++ )
    
{
        
puts ( qwords [ i ]) ;
    
}
 
    
return 0 ;
}

注意,在这里函数复制的字符串可能最后没有空字符,所以你必须手动在最后放置一个空字符,而且注意这里是复制n个字符过去,所以要留一个给空字符

,所以在这里是TARGSIZE - 1;

strncpy ( qwords [ i ] , temp , TARGSIZE - 1 ) ;
qwords [ i ][ TARGSIZE - 1 ] = '\ 0 ';

5.sprintf ()函数
sprintf ()函数是在stdio.h而不是在string.h中声明的。
它的作用和printf ()一样,但是它写到字符串里而不是写到输出显示。
它提供了把几个元素组合成一个字符串的一种途径。
sprintf ()的第一个参数是目标字符串地址。

//format.c
#include < stdio.h >
#define MAX 20
 
int main ( void )
{
    
char first [ MAX ] ;
    
char last [ MAX ] ;
    
char formal [ 2 * MAX + 10 ] ;
    
double prize ;
 
    
puts ( " Enter your first name: " ) ;
    
gets ( first ) ;
    
puts ( " Enter your last name: " ) ;
    
gets ( last ) ;
    
puts ( " Enter your prize money: " ) ;
    
scanf ( " %1f " , prize ) ;
    
sprintf ( formal , " %s, %-19s: $%6.2f \ n " , last , first , prize ) ;
    
puts ( formal ) ;
 
    
return 0 ;
}

6.其它字符串函数

char * strcpy ( char * s1 , const char * s2 ) ;

该函数把s2指向的字符串(包括空字符)复制到s1指向的位置,返回值是s1。

char * strncpy ( char * s1 , const char * s2 , size_t n ) ;

该函数把s2指向的字符串(包括空字符)复制到s1指向的位置,复制的字符数不超过n个。返回值是s1。
空字符后的字符串不被复制。
如果源字符串的字符数小于n个,在目标字符串中就以空字符填充。
如果源字符串的字符数大于或等于n个,空字符就不被复制。返回值是s1。

char * strcat ( char * s1 , const char * s2 ) ;

s2指向的字符串(包括空字符)复制到s1指向的结尾。复制过来的s2覆盖了s1的空字符。返回值为s1。

char * strncat ( char * s1 , const char * s2 ) ;

s2指向的字符串中的前n个字符(包括空字符)复制到s1指向的结尾。复制过来的s2覆盖了s1的空字符。
s2字符串中的空字符及其后的任何字符都不会被复制,并且追加一个空字符到所得结果后面。
返回值为s1。

int strcmp ( const char * s1 , const char * s2 ) ;

如果s1字符串在机器编码顺序中落后于s2字符串,函数的返回值是一个正数;如果两个字符串相同,返回值是0;
如果第一个字符串在机器编码顺序中先于第二个字符串,返回值是一个负数。

int strncmp ( const char * s1 , const char * s2 ) ;

该函数作用和strcmp ()一样,只是比较n个字符后或者遇见第一个空字符时会停止比较。

char * strchr ( const char * s1 , int c ) ;

该函数返回一个指向字符串s中存放字符c的第一个位置的指针(标志结束的空字符是字符串的一部分,因此也可以搜索到它)。
如果没有找到该字符,函数就返回空指针。

char * strpbrk ( const char * s1 , const char * s2 ) ;

该函数返回一个指针,指向字符串s1中存放s2字符串的任何字符的第一个位置。如果没有找到该字符。如果没找到任何字符,返回空指针。

char * strrchr ( const char * s , int c ) ;

该函数返回一个指针,指向字符串s中字符c最后一次出现的地方。(标志结束的空字符是字符串的一部分,因此也可以搜索到它)。
如果没有找到该字符,函数就返回空指针。

char * strstr ( const char * s1 , const char * s2 )

该函数返回一个指针,指向s1字符串中第一次出现s2字符串的地方。如果在s1中没找到s2字符串,函数就返回空指针。

size_t strlen ( const char * s ) ;

返回字符串的长度。

简单应用:
在用fgets ()函数时,读取一行输入,这个函数把换行符存储到目标字符串中。
我们可以使用strchr ()函数来用一个空字符替换这个换行符。

char line [ 80 ] ;
char * find ;
 
fgets ( line , 80 , stdin ) ;
find = strchr ( line , '\ n ' ) ;
if ( find )
{
    *
find = '\ 0 ';
}

你可能感兴趣的:(字符串函数(三))