亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來(lái)到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? 第6章 字符串操作.txt

?? 我自己整理的c語(yǔ)言教程 來(lái)自 c語(yǔ)言之家
?? TXT
?? 第 1 頁(yè) / 共 2 頁(yè)
字號(hào):
C語(yǔ)言編程常見(jiàn)問(wèn)題解答  
 

 
發(fā)表日期:2003年9月30日      已經(jīng)有2342位讀者讀過(guò)此文 
 


                                     第6章  字符串操作

    本章集中討論字符串操作,包括拷貝字符串,拷貝字符串的一部分,比較字符串,字符串右對(duì)齊,刪去字符串前后的空格,轉(zhuǎn)換字符串,等等。C語(yǔ)言提供了許多用來(lái)處理字符串的標(biāo)準(zhǔn)庫(kù)函數(shù),本章將介紹其中的一部分函數(shù)。
    在編寫C程序時(shí),經(jīng)常要用到處理字符串的技巧,本章提供的例子將幫助你快速學(xué)會(huì)一些常用函數(shù)的使用方法,其中的許多例子還能有效地幫助你節(jié)省編寫程序的時(shí)間。

    6.1 串拷貝(strcpy)和內(nèi)存拷貝(memcpy)有什么不同?它們適合于在哪種情況下使用?
    strcpy()函數(shù)只能拷貝字符串。strcpy()函數(shù)將源字符串的每個(gè)字節(jié)拷貝到目錄字符串中,當(dāng)遇到字符串末尾的null字符(\0)時(shí),它會(huì)刪去該字符,并結(jié)束拷貝。
    memcpy()函數(shù)可以拷貝任意類型的數(shù)據(jù)。因?yàn)椴⒉皇撬械臄?shù)據(jù)都以null字符結(jié)束,所以你要為memcpy()函數(shù)指定要拷貝的字節(jié)數(shù)。
      在拷貝字符串時(shí),通常都使用strcpy()函數(shù);在拷貝其它數(shù)據(jù)(例如結(jié)構(gòu))時(shí),通常都使用memcpy()函數(shù)。
    以下是一個(gè)使用strcpy()函數(shù)和memcpy()函數(shù)的例子:

#include <stdio. h>
#include <string. h>
typedef struct cust-str {
    int id ;
    char last_name [20] ;
    char first_name[l5];
} CUSTREC;
void main (void);
void main (void)
{
    char * src_string = "This is the source string" ;
    char dest_string[50];
    CUSTREC src_cust;
    CUSTREC dest_cust;
    printf("Hello! I'm going to copy src_string into dest_string!\n");
    / * Copy src_ string into dest-string. Notice that the destination
      string is the first argument. Notice also that the strcpy()
      function returns a pointer to the destination string. * /
    printf("Done! dest_string is: %s\n" ,
        strcpy(dest_string, src_string)) ;
    printf("Encore! Let's copy one CUSTREC to another. \n") ;
    prinft("I'll copy src_cust into dest_cust. \n");
    / * First, intialize the src_cust data members. * /
    src_cust. id = 1 ;
    strcpy(src_cust. last_name, "Strahan");
    strcpy(src_cust. first_name, "Troy");
    / * Now, Use the memcpy() function to copy the src-cust structure to
        the dest_cust structure. Notice that, just as with strcpy(), the
        destination comes first. * /
    memcpy(&dest_cust, &src_cust, sizeof(CUSTREC));
    printf("Done! I just copied customer number # %d (%s %s). " ,
        dest_cust. id, dest_cust. first_name, dest_cust. last_name) ;
}

    請(qǐng)參見(jiàn):    
    6.6怎樣拷貝字符串的一部分?
    6.7怎樣打印字符串的一部分?

    6. 2怎樣刪去字符串尾部的空格?。
    C語(yǔ)言沒(méi)有提供可刪去字符串尾部空格的標(biāo)準(zhǔn)庫(kù)函數(shù),但是,編寫這樣的一個(gè)函數(shù)是很方便的。請(qǐng)看下例:
#include <stdio. h>
# include <string. h>

void main (void);
char * rtrim(char * );
void main(void)
{
    char * trail_str = "This string has trailing spaces in it";
   / * Show the status of the string before calling the rtrim()
       function. * /
    printf("Before calling rtrim(), trail_str is '%s'\fi" , trail_str);
    print ("and has a length of %d. \n" , strlen (trail_str));
   / * Call the rtrimO function to remove the trailing blanks. * /
    rtrim(trail_str) ;
   / * Show the status of the string
       after calling the rtrim() function. * /
    printf("After calling rttim(), trail_ str is '%s'\n", trail _ str );
    printf ("and has a length of %d. \n" , strlen(trail-str)) ;
}
/ * The rtrim() function removes trailing spaces from a string. * /.

char * rtrim(char * str)
{
    int n = strlen(str)-1; / * Start at the character BEFORE
                               the null character (\0). * / 
    while (n>0) / * Make sure we don't go out of hounds. . . * /
    {
        if ( * (str + n) 1 =' ') / * If we find a nonspace character: * /
        {
            * (str+n+1) = '\0' ; / * Put the null character at one
                                     character past our current
                                     position. * /
            break ; / * Break out of the loop. * /
        }
        else / * Otherwise , keep moving backward in the string. * /.
            n--;
    }
    return str;    /*Return a pointer to the string*/
}

    在上例中,rtrim()是用戶編寫的一個(gè)函數(shù),它可以刪去字符串尾部的空格。函數(shù)rtrim()從字符串中位于null字符前的那個(gè)字符開(kāi)始往回檢查每個(gè)字符,當(dāng)遇到第一個(gè)不是空格的字符時(shí),就將該字符后面的字符替換為null字符。因?yàn)樵贑語(yǔ)言中null字符是字符串的結(jié)束標(biāo)志,所以函數(shù)rtrim()的作用實(shí)際上就是刪去字符串尾部的所有空格。

    請(qǐng)參見(jiàn):    
    6.3怎樣刪去字符串頭部的空格?
    6.5怎樣將字符串打印成指定長(zhǎng)度?
    
    6.3  怎樣刪去字符串頭部的空格?
    C語(yǔ)言沒(méi)有提供可刪去字符串頭部空格的標(biāo)準(zhǔn)庫(kù)函數(shù),但是,編寫這樣的一個(gè)函數(shù)是很方便的。請(qǐng)看下例;
#include <stdio. h> 
#include <string. h>

void main(void); 
char *  ltrim (char * ) ; 
char * rtrim(char * ) ;
void main (void)
{
    char * lead_str = " This string has leading spaces in it. " ;,
    / * Show the status of the string before calling the Itrim()
        function. * /
    printf("Before calling Itrim(), lead-str is '%s'\n", lead_str);
    printf("and has a length of %d. \n" , strlen(lead_str));
    / * Call the Itrim() function to remove the leading blanks. * /.
        Itrim(lead_str);
    / * Show the status of the string
        after calling the Itrim() function. * /
    prinft("After calling Itrim(), lead_str is '%s'\n", lead_str);
    print("and has a length of %d. \n'' , strlen(lead-str)) ;
}
/ * The Itrim() function removes leading spaces from a string. * /

char * ltrim(char * str)
{
    strrev(str) ; / * Call strrevO to reverse the string. * /
    rtrim(str)). /* Call rtrimO to remvoe the "trailing" spaces. * /
    strrev(str); / * Restore the string's original order. * /
    return str ; / * Return a pointer to the string. * /.
}
/ * The rtrim() function removes trailing spaces from a string. * /

char* rtrim(char* str)
{
    int n = strlen (str)-l ; / * Start at the character BEFORE
    the null character (\0). * /
    while (n>0) / * Make sure we don't go out of bounds... * /.
    {
        if ( * (str+n) ! =' ') If we find a nonspace character: * /
        {
            * (str+n + 1) = '\0' ; / * Put the null character at one
            character past our current
            position. * /
            break;j / * Break out of the loop. * /
        }
        else / * Otherwise, keep moving backward in the string. * /
            n --;
    }
    return  str;    /*  Return a pointer tO the string.  */
}

    在上例中,刪去字符串頭部空格的工作是由用戶編寫的ltrim()函數(shù)完成的,該函數(shù)調(diào)用了·6.2的例子中的rtrim()函數(shù)和標(biāo)準(zhǔn)C庫(kù)函數(shù)strrev()。ltrim()函數(shù)首先調(diào)用strrev()函數(shù)將字符串顛倒一次,然后調(diào)用rtrim()函數(shù)刪去字符串尾部的空格,最后調(diào)用strrev()函數(shù)將字符串再顛倒一次,其結(jié)果實(shí)際上就是刪去原字符串頭部的空格。

    請(qǐng)參見(jiàn):
    6.2怎樣刪去字符串尾部的空格?    
    6.5怎樣將字符串打印成指定長(zhǎng)度?  

    6.4  怎樣使字符串右對(duì)齊?    
    C語(yǔ)言沒(méi)有提供可使字符串右對(duì)齊的標(biāo)準(zhǔn)庫(kù)函數(shù),但是,編寫這樣的一個(gè)函數(shù)是很方便的。請(qǐng)看下例:
#include <stdio. h>
#include <string. h>
#include <malloc. h>

void main (void);
char * r just (char * ) ;
char * rtrim(char * );
void main (void)
{
    char * rjust_str = "This string is not righ-justified. " ;
    / * Show the status of the string before calling the rjust()
        function. * /
    printf("Before calling rjust(), rjust_str is ' %s'\n. " , rjust_str);
    / * Call the rjustO function to right-justify this string. * /
    rjust(rjust_str) ;
    / * Show the status of the string
        after calling the rjust() function. * /
    printf ("After calling rjust() , rjust_str is ' %s'\n. " , rjust_str) ;
}
/ * The rjust() function right-justifies a string. * /
char * r just (char * str)
{
    int n = strlen(str); / * Save the original length of the string. * /
    char* dup_str;
    dup_str = strdup(str); / * Make an exact duplicate of the string. * /
    rtrim(dup_str); /* Trim off the trailing spaces. */
    / * Call sprintf () to do a virtual "printf" back into the original
        string. By passing sprintf () the length of the original string,
        we force the output to be the same size as the original, and by
        default the sprintf() right-justifies the output. The sprintf()
        function fills the beginning of the string with spaces to make
        it the same size as the original string. * /

    sprintf(str, "%*. * s", n, n, dup_str);

    free(dup-str) ; / * Free the memory taken by
    the duplicated string. * /
    return str;\ / * Return a pointer to the string. * /
}

/ * The rtrim() function removes trailing spaces from a string. * /
char * rtrim(char * str)
{
    int n = strlen(str)-l; / * Start at the character BEFORE the null
                               character (\0). * /
    while (n>0) / * Make sure we don't go out of bounds... * /
    {
        if ( * (str+n) ! = ' ') / * If we find a nonspace character: * /
        {
            * (str + n + 1) = '\0';( / * Put the null character at one
                                         character past our current
                                         position. * /
             break; / * Break out of the loop. * /
        }
        else / * Otherwise, keep moving backward in the string. * /
            n—;
    }
    return str ; / * Return a pointer to the string. * /
}
  
   在上例中,使字符串右對(duì)齊的工作是由用戶編寫的rjust()函數(shù)完成的,該函數(shù)調(diào)用了6.2的例子中的rtrim()函數(shù)和幾個(gè)標(biāo)準(zhǔn)函數(shù)。rjust()函數(shù)的工作過(guò)程如下所示:
    (1) 將原字符串的長(zhǎng)度存到變量n中。這一步是不可缺少的,因?yàn)檩敵鲎址驮址拈L(zhǎng)度必須相同。
    (2) 調(diào)用標(biāo)準(zhǔn)C庫(kù)函數(shù)strdup(),將原字符串復(fù)制到dup_str中。原字符串需要有一份拷貝,因?yàn)榻?jīng)過(guò)右對(duì)齊處理的字符串要寫到原字符串中。
    (3) 調(diào)用rtrim()函數(shù),刪去dup_str尾部的空格。
    (4) 調(diào)用標(biāo)準(zhǔn)C庫(kù)函數(shù)sprinf(),將dup_str寫到原字符串中。由于原字符串的長(zhǎng)度(存在n中)被傳遞給sprintf()函數(shù),所以迫使輸出字符串的長(zhǎng)度和原字符串相同。因?yàn)閟printf()函數(shù)缺省使輸出字符串右對(duì)齊,因此輸出字符串的頭部將被加入空格,以使它和原字符串長(zhǎng)度相同,其效果實(shí)際上就是使原字符串右對(duì)齊。
    (5)調(diào)用標(biāo)準(zhǔn)庫(kù)函數(shù)free(),釋放由strdup()函數(shù)分配給dup_str的動(dòng)態(tài)內(nèi)存。

    請(qǐng)參見(jiàn):    
    6.5怎樣將字符串打印成指定長(zhǎng)度?    

    6.5  怎樣將字符串打印成指定長(zhǎng)度?    
    如果要按表格形式打印一組字符串,你就需要將字符串打印成指定長(zhǎng)度。利用printf()函數(shù)可以很方便地實(shí)現(xiàn)這一點(diǎn),請(qǐng)看下例:

# include <stdio. h>
char * data[25] = {
    "REGION", "--Q1--", "--Q2--", "--Q3--", "--Q4--",
    "North" , "10090. 50" , "12200. 10" , "26653.12" , "62634. 32" ,
    "South", "21662.37", "95843.23", "23788.23", "48279.28",
    "East", "23889.38", "23789.05", "89432.84", "29874.48",
    "West", "85933.82", "74373.23", "78457.23", "28799.84" };
void main (void) ;
void main (void)
{
    int x;
    fox (x = 0, x<25; x+ + )
    {
        if ((x % 5) == 0&&(x !=0))
        printf("\n");
        printf (" %-10. 10s" , data[x]) ;
    }
}
  
    在上例中,字符串?dāng)?shù)組char *data[]中包含了某年4個(gè)地區(qū)的銷售數(shù)據(jù)。顯然,你會(huì)要求按表格形式打印這些數(shù)據(jù),而不是一個(gè)挨一個(gè)地毫無(wú)格式地打印這些數(shù)據(jù)。因此,上例中用下述語(yǔ)句來(lái)打印這些數(shù)據(jù):
    printf("%-10.10s",data[x]);
    參數(shù)"%-10.10s"指示printf()函數(shù)按10個(gè)字符的長(zhǎng)度打印一個(gè)字符串。在缺省情況下,printf()函數(shù)按右對(duì)齊格式打印字符串,但是,在第一個(gè)10的前面加上減號(hào)(-)后,prinft()函數(shù),就會(huì)使字符串左對(duì)齊。為此,printf()函數(shù)會(huì)在字符串的尾部加入空格,以使其長(zhǎng)度達(dá)到10個(gè)字符。上例的打印輸出非常整潔,類似于一張表格,如下所示:

    REGION    --Q1--    --Q2--     --Q3--     --Q4--

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产乱子伦视频一区二区三区 | 色哟哟在线观看一区二区三区| 丝袜美腿一区二区三区| 中文字幕在线免费不卡| 国产日韩影视精品| 久久精品人人做人人爽人人| 欧美精品自拍偷拍动漫精品| 在线视频观看一区| 欧美日韩一区二区三区四区| 欧美电影一区二区| 欧美一级二级三级乱码| 日韩一区二区影院| 久久久久久久电影| 国产精品乱码一区二三区小蝌蚪| 国产欧美一区二区三区在线老狼| 欧美国产禁国产网站cc| 国产精品短视频| 夜夜精品视频一区二区| 午夜精品成人在线| 久久99精品网久久| 成人高清av在线| 色偷偷一区二区三区| 欧美日韩国产天堂| 欧美成人a视频| 国产精品欧美精品| 亚洲成a人片在线观看中文| 日本不卡123| 国产白丝精品91爽爽久久 | 欧美日韩国产首页在线观看| 91精品国产欧美一区二区成人| www成人在线观看| 久久欧美一区二区| 亚洲免费在线观看| 久久国产精品区| 91色九色蝌蚪| 欧美一区二区福利视频| 中文字幕第一区第二区| 亚洲成a人片在线观看中文| 精品系列免费在线观看| 一本久道久久综合中文字幕| 欧美一区二区三区在线观看| 中文文精品字幕一区二区| 亚洲免费资源在线播放| 黑人巨大精品欧美一区| 欧美天天综合网| 国产无人区一区二区三区| 亚洲地区一二三色| 丰满少妇在线播放bd日韩电影| 欧美日韩精品专区| 中文字幕日韩av资源站| 久久精品国产色蜜蜜麻豆| 一本一道久久a久久精品| 久久你懂得1024| 三级成人在线视频| av动漫一区二区| 国产美女视频91| 不卡电影一区二区三区| 欧美精品免费视频| 中文字幕一区二区三区在线不卡| 免费视频一区二区| 欧美天堂一区二区三区| 日韩理论片在线| 国产成人自拍在线| 久久久精品2019中文字幕之3| 亚洲va国产va欧美va观看| 91丝袜呻吟高潮美腿白嫩在线观看| 精品少妇一区二区| 青草国产精品久久久久久| 欧美吞精做爰啪啪高潮| 亚洲一区二区中文在线| 色婷婷综合久久久久中文一区二区 | 亚洲一区二区四区蜜桃| 99久久久免费精品国产一区二区| 久久午夜国产精品| 韩国欧美国产1区| 精品三级av在线| 免费成人深夜小野草| 69堂亚洲精品首页| 婷婷一区二区三区| 777午夜精品免费视频| 天堂午夜影视日韩欧美一区二区| 欧美在线观看视频一区二区| 亚洲一区二区不卡免费| 在线观看91视频| 亚洲国产人成综合网站| 欧美精品一卡两卡| 麻豆免费看一区二区三区| 日韩久久久久久| 国产精品白丝av| 国产精品毛片大码女人| 91成人在线观看喷潮| 天堂va蜜桃一区二区三区漫画版| 欧美一区二区三区啪啪| 久久av资源网| 国产精品美女久久久久久2018| 欧美专区亚洲专区| 亚洲午夜激情网页| 日韩欧美一区二区三区在线| 国产精品中文字幕日韩精品| 中文字幕国产一区二区| 91论坛在线播放| 五月婷婷激情综合网| 精品久久久久久久久久久久久久久久久 | 人人爽香蕉精品| 久久亚洲欧美国产精品乐播| 99这里都是精品| 日本aⅴ精品一区二区三区 | 日本丶国产丶欧美色综合| 午夜精品成人在线| 久久人人97超碰com| 色一情一乱一乱一91av| 男男成人高潮片免费网站| 中文字幕精品一区二区三区精品| 婷婷亚洲久悠悠色悠在线播放| 欧美性一区二区| 精品一区二区在线视频| 亚洲国产成人午夜在线一区| 欧美群妇大交群中文字幕| 国产69精品久久久久777| 亚洲精品视频在线观看免费| 欧美一区二区在线看| 99综合影院在线| 精久久久久久久久久久| 一区二区在线观看不卡| xfplay精品久久| 欧美日韩精品一区二区三区| 国产精品亚洲午夜一区二区三区| 午夜精品久久久久久久久久久| 中文一区一区三区高中清不卡| 91精品国产高清一区二区三区 | 国产精品久久久久久久久免费丝袜 | 日韩精品中文字幕一区| 色哟哟一区二区三区| 高清不卡一区二区| 久久精品国产久精国产爱| 亚洲午夜免费电影| 国产精品乱码一区二三区小蝌蚪| 精品日韩在线一区| 日韩欧美在线网站| 欧美日韩久久久久久| 91激情五月电影| 91在线视频播放地址| 国产精品一区在线观看你懂的| 日韩成人av影视| 日韩有码一区二区三区| 亚洲午夜久久久久中文字幕久| 亚洲天堂a在线| 一区二区中文字幕在线| 亚洲精品久久久久久国产精华液 | 国产精品综合在线视频| 舔着乳尖日韩一区| 一区二区三区四区精品在线视频| 亚洲欧洲国产日本综合| 国产精品免费观看视频| 亚洲国产精品ⅴa在线观看| 久久先锋影音av| 久久无码av三级| 国产视频一区在线观看| 国产喷白浆一区二区三区| 国产三级欧美三级| 中文字幕在线不卡视频| 亚洲嫩草精品久久| 一区二区三区不卡视频| 亚洲一区二区三区视频在线| 亚洲黄网站在线观看| 亚洲一本大道在线| 视频一区中文字幕| 麻豆精品国产91久久久久久| 国产精品一区2区| 成人免费的视频| 色拍拍在线精品视频8848| 在线观看日韩国产| 91精品国模一区二区三区| 日韩美一区二区三区| 中文字幕av一区二区三区免费看 | 国产日韩欧美精品综合| 国产精品你懂的在线| 一区二区三区不卡视频| 日韩1区2区3区| 国产馆精品极品| 色综合久久综合网97色综合| 欧美老女人第四色| 国产亚洲欧美中文| 日本中文字幕不卡| 国产一区久久久| 色综合一个色综合亚洲| 欧美一区二区三区人| 国产精品欧美一级免费| 午夜不卡在线视频| 顶级嫩模精品视频在线看| 日本精品裸体写真集在线观看| 欧美一区二区三区在| 国产精品美女久久久久高潮| 日韩成人伦理电影在线观看| av中文字幕不卡| 欧美大片免费久久精品三p| 亚洲精品精品亚洲| 国产一区福利在线| 欧美日韩一区国产| 国产精品无人区|