C語言基礎總結 ( 三 )----------字串總結

來源:互聯網
上載者:User

標籤:

??字串處理函數#include <string.h> // 字串數組標頭檔    // puts輸出函數    char str[20] = "zyp";    printf("who loves %s\n", str); // 不會自動換行    puts(str);    puts("gj"); // 不能進行格式化輸出       // gets輸入函數
    char str2[15];
    gets(str2); // 使用scanf時不能有空格,gets中間可以有空格
    puts(str2);       // strcat   字串串連函數
    // 返回的是前面的字串的地址,儲存在前面的字串數組中
    strcat(str, str2);
    puts(str);
    printf("串連後的字串為%s\n", str);       // strcpy   字串拷貝函數    // strcpy(str1, str2)   把字串2拷貝到字串1中,字串1中的內容被覆蓋                    str1的長度 >= str2的長度    strcpy(str, str2);
    puts(str);       // strcmp   字串比較函數
    char str3[] = "ac";
    char str4[] = "fun";
    printf("%d\n", strcmp(str3, str4));       // strlen   計算字串的長度    char str5[] = "德瑪西亞";   // 一個漢字預設是 utf-8 編碼,佔用3個位元組    printf("%lu\n", strlen(str5));  ??單字首大寫統計單詞個數    // 首先定義一個字串數組
    char str[100];
    // 定義變數count統計單詞的個數
    int count = 0;
    int isWord = 1; // 用來判別是不是單詞
    // 輸入字串
    gets(str);
    // 迴圈判斷字元是不是\0
    for (int i = 0; str[i] != ‘\0‘; i++) {
        // 再判斷是不是單詞
        if (str[i] == ‘ ‘) {
            isWord = 1; // 等於空格說明這是一個單詞的開始
        }else if (isWord == 1){
            str[i] = str[i] - 32;
            count++;
            isWord = 0; // 迴圈得到一個單詞後就把isword歸0
        }
    }     printf("%d\n", count);    puts(str);

C語言基礎總結 ( 三 )----------字串總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.