字符型數據/字符串數據


字符型數據包括字符常量和字符變量。

字符常量   -- %c  char占8位

字符常量是用單引號括起來的一個字符。

例如:'a'、 'b' 、'='、'+'、 '?'

都是合法字符常量

在c語言中, 字符常量有一下特點:

1)字符常量只能用單引號括起來, 不能用雙引號或其他括號。

2)字符常量只能是單個字符, 不能是字符串

3)字符可以是字符集中任意字符。 但數字被定義字符型后不能參與運算, 如5 和'5'

字符變量 ---不全等看完c plus在補充

字符變量用來存儲字符常量, 即單個字符。

字符變量的類型說明符是char。字符變量類型定義的格式和書寫規則都與整形變量相同。

例如:

 

 

#include <stdio.h>

int main(){
    char ch;
    int len = 0;
    printf("Enter a message: ");
    ch = getchar();
    while (ch != '\n'){
        len++;
        ch = getchar();
    }
    printf("Your message was %d character(s) long.\n", len);
}

 

 字符數組

字符串常量是由一對雙引號括起來的字符序列。 "hello world" 

字符串常量與字符常量的是不同的量。

區別:

1. 字符常量由單引號括起來, 字符串常量由雙引號括起來。

2. 字符常量只能是單個字符, 字符串常量則可以含一個或多個字符。

3. 可以把一個字符常量賦予一個字符變量, 但不能把一個字符串常量賦予一個字符變量

4. 字符常量占一個字節的內存空間。 字符串常量占的內存字節數等於字符串中字節數加1。增加的一個字節中存放字符"\0",這是字符串結束標志。記住是0, 不是o, 是01的0

char s[]="dsfdsfsdf"
char s[]={'a', 'b', 'c'};

 

轉義字符

轉義字符是一種特殊的字符常量。轉移字符以反斜線'\'開頭, 后跟一個

或幾個字符。轉義字符具有特定的含義, 不同於字符原有的意義, 故稱“轉義”字符。

例如, 在前面各例題中printf函數的格式串中用到的"\n"就是一個轉義字符, 其意義是'回車換行"。轉義字符

主要用來表示那些用一般字符不便於表示的控制代碼。

 

強制類型轉換

強制類型轉換是通過類型轉換運算來實現的。

其一般形式為:

(類型說明符)(表達式)

其功能是把表達式的運算結果強制轉換成類型說明符所表示的類型。

例如:

(float)a 把a轉換成浮點型

(int)(x+y) 把x+y的結果轉換為整型

在使用強制轉換時應注意一下問題:

1)類型說明符和表達式都必須加括號(單個變量可以不加括號,)如把(int)(x+y)

寫成(int)x+y則成為了x專場成int型之后再與y相加了

2)無論是強制轉換或是自動轉換, 都只是為了本次運算的需要而對變量的數據長度進行的臨時性轉換,而不是改變數據說明時對該變量定義的類型

void main()
{
    char a;
    int b;
    a = '1';
    b = 22;
    printf("%f %d", (float)(b), (int)(a));
}

 

算數運算和表達式

%  得到余數  7 % 2 得到1

 

    

字符串

 

 

 

 

 

 

方法一:

定義字符串數組

#define  STR_LEN 80
    char str[STR_LEN + 1] = "abcd";
    printf("%s", str);

 

方法二:

通過字符指針的方式:

在聲明指針的適合,指向字符串字面量,不可以修改字符串字面量,數組可改。
char
*a; a = "test"; printf("%s", a); /*test*/

 

字符串的非法操作

 

字符串復制 strcpy函數

原型

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

strcpy函數把s2中的字符串復制給了字符串s1,直到遇到‘\0’;

#include <stdio.h>
#include <string.h>


int main(void) {
    char *str1 = "abcd", *str2;
    strcpy(str2, str1);
    printf("%s", str2); //abcd
    return 0;
}

 

 

c語言字符串拼接 strcat

#include <stdio.h>
#include <string.h>
int main()
{
    char a[100] = "say";
    char *c = "hello";
    strcat(a, c);
    printf("%s", a);
    return 0;
}

 

為什么c語言字符串拼接不能用指針的方式呢?

為什么下面的不行呢?

strcat是沒有重新申請空間的,只是直接將前面的參數的最后一個'\0'去掉,然后講第二個參數的字符直接添上去,再在末尾補'\0',所以前面的空間至少有下str1+str2+1空間。

#include <stdio.h>
#include <string.h>


int main(void) {
    char *str1 = "hello ", *str2 = "world!!!";
    strcat(str1, str2);
    printf("%s", str1); //abcd
    return 0;
}

 

c語言字符串追加 strncat 

char * strncat(char *dest, const char *src, size_t n);

解釋:

  把src所指字符串的前n個字符添加到dest所指字符串的結尾處,並覆蓋dest所指字符串結尾的'\0',從而實現字符串的連接。

注意:

  拼接的時候要有足夠的空間

示例:

#include <stdio.h>
#include <string.h>

int main(){

    char ch1[10] = "hello";
    char ch2[5+1] = "world";
    strncat(ch1, ch2, 3);
    printf("%s\n", ch1); //結果是:hellowor
    return 0;
}

 

c語言字符串比較 strcmp

#include <stdio.h>
#include <string.h>


int main(void) {
    char *str1 = "hello adfadf", *str2 = "world!!!";
    int i;
    i = strcmp(str1, str2);
    printf("%d", i); // -15,為啥我也不知道

    //printf("%s", str1); //abcd
    return 0;
}

 

 當str1 == str2的時候結果為0

#include <stdio.h>
#include <string.h>

int main(){
    printf("hello world!!!\n");

    char *str1 = "hello", *str2 = "hello";
    int i = strcmp(str1, str2);
    printf("%d",i);
    return 0;
}

 

當str1 > str2 的時候,正數

#include <stdio.h>
#include <string.h>

int main(){
    printf("hello world!!!\n");

    char *str1 = "hello1", *str2 = "hello";
    int i = strcmp(str1, str2);
    printf("%d",i);
    return 0;
}

 

當str1 < str2 的時候, 負數

#include <stdio.h>
#include <string.h>

int main(){
    printf("hello world!!!\n");

    char *str1 = "hello", *str2 = "hello2";
    int i = strcmp(str1, str2);
    printf("%d",i);
    return 0;
}

 

c語言字符串區間比較 strncmp,高級玩法

#include <stdio.h>
#include <string.h>

int main(){

    char a[10] = "abcdefg";
    printf("%s\n", a);
    int i;
    i = strncmp(a, "abc", 3); // 意思是從a+0的地方到a+2的地方比較
    printf("%d\n",

 

 

c語言求字符串長度 strlen函數

#include <stdio.h>
#include <string.h>


int main(void) {
    char *str1 = "hello world!!!";
    int i;
    i = strlen(str1);
    printf("%d", i); // 14
    return 0;
}

 

 

 

字符串數組的兩種寫法 

但是這樣只適合直接創建賦值時候用;

 

 

方法一:

 

方法二:

 

 

字符串示例,比較不錯的學習例子, 字符串數組的操作

 

#include <stdio.h>
#include <string.h>

#define MAX_REMIND 50
#define MSG_LEN 60

int read_line(char str[], int n);

int main() {

    char reminders[MAX_REMIND][MSG_LEN + 3]; //定義二維數組, 可以輸入50個樣本
    char day_str[3], msg_str[MSG_LEN + 1]; // 定義時間, 消息;
    int day, i, j, num_remind = 0;

    for (;;) {  // 無限循環
        if (num_remind == MAX_REMIND) {    // 如果達到次數,break,結束不輸入了
            printf("--No space left --\n");
            break;
        }
        printf("Enter day and reminder: ");
        scanf("%2d", &day); //輸入天數

        if (day == 0) //輸入0結束
            break;
        sprintf(day_str, "%2d", day); //強制格式化時間為字符串;
//        printf("day_str: %s\n", day_str);
//        printf("num_remind: %d\n", num_remind);
        read_line(msg_str, MSG_LEN); //消息和字符串,字符數組;

        for (i = 0; i < num_remind; i++) {
            if (strcmp(day_str, reminders[i]) < 0) //校驗,如果轉換的日期和輸入的不一樣,結束
                break;
        }

        strcpy(reminders[i], day_str); // 復制字符串
        printf("i: %s \n", reminders[i]);
        strcat(reminders[i], msg_str); // 合並字符串
        printf("i: %s \n", reminders[i]);

        num_remind++;
    }
    printf("\nDay Reminder \n");
    for (i = 0; i < num_remind; i++) { //最后循環打印
        printf(" %s\n", reminders[i]);
    }


    return 0;
}

int read_line(char str[], int n) {
    char ch;
    int i = 0;

    while ((ch = getchar()) != '\n') {
        if (i < n)
            str[i++] = ch;
    }
    str[i] = '\0';
    return i;
}

 

 

c語言字符串替換,非常不錯的例子

 是不是和python的replace很像呀

編寫名為censor的函數,用來把字符串中的出現的每一處字符“foo”

#include <stdio.h>
#include <string.h>

char *strrpc(char *str, char *oldstr, char *newstr);

int main() {

    char p[] = "hello world.";
    printf("替換前:%s\n", p);
    printf("替換后:%s\n", strrpc(p, ".", "!!!"));
    return 0;
}

char *strrpc(char *str, char *oldstr, char *newstr) {
    char bstr[strlen(str)];//轉換緩沖區
    memset(bstr, 0, sizeof(bstr)); //內存重置

    for (int i = 0; i < strlen(str); i++) {
        /*查找目標字符串, 因為需要從某str+i -->str + i + strlen(oldstr)區間的字符和oldstr進行比較*/
        /*如果==0,則進行拼接*/
        if (!strncmp(str + i, oldstr, strlen(oldstr))) {
            strcat(bstr, newstr);
            i += strlen(oldstr) - 1; //跳到對比字符的后面繼續,這里為什么要減1呢,因為索引從0開始呀;
        } else {
            /*如果沒匹配成果,則進行指定多長的字節進行拼接,這里的1是拼接一個字符*/
            strncat(bstr, str + i, 1);//這里的1是size_t n,拼接幾個長度
        }
    }

    strcpy(str, bstr);
    return str;
}

============================
替換前:hello world.
替換后:hello world!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM