1. 程式人生 > >c練習題(1)字串操作練習題

c練習題(1)字串操作練習題

1、有一個字串開頭或結尾含有n個空格(”   abcdefgdddd    ”),欲去掉前後空格,返回一個新字串。

要求1:請自己定義一個介面(函式),並實現功能;70分

要求2:編寫測試用例。30分

int trimSpace(char *inbuf, char *outbuf); 

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

//先去掉前面的空格,再去掉後面的空格
int trimSpace(char *inbuf, char *outbuf){
    char *p = inbuf;
    //檢視有多少個空格
    int len = 0;
    while(p[len] == ' '){
        len++;
    }

    //將字串進行前移
    int i = len;
    while(p[i]){
        p[i - len] = p[i];
        i++;
    }
    p[i - len] = '\0';

    //去掉後面的字串
    for(int j = 0; j < strlen(p); j++){
        if(p[j] == ' '){
            p[j] = '\0';
            outbuf = p;
            return 1;
        }
    }

    return 0;
}


int main(){
    char str[] = "        abcdefgdddd        ";
    char *outbuf = NULL;

    if(trimSpace(str, outbuf)){
        printf("start.%s.end", str);
    }else{
        printf("No need for trim.");
    }

}

 

2、有一個字串”1a2b3d4z”,;

要求寫一個函式實現如下功能,

功能1:把偶數位字元挑選出來,組成一個字串1。valude;20分

功能2:把奇數位字元挑選出來,組成一個字串2,valude 20

功能3:把字串1和字串2,通過函式引數,傳送給main,並列印。

功能4:主函式能測試通過。

int getStr1Str2(char *souce, char *buf1, char *buf2);

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

//輸入字串,輸出兩個擷取之後的字串
int getStr1Str2(char *str, char *buf1, char *buf2){
    char *p = str;
    int temp1 = 0;
    int temp2 = 0;

    if(p == NULL){
        return -1;
    }

    for(int i = 0; i < strlen(p); i++){
        if(i % 2 == 0){
            buf1[temp1] = p[i];
            temp1++;
        }else{
            buf2[temp2] = p[i];
            temp2++;
        }
    }

    return 1;
}

int main(){
    char str[] = "1a2b3d4z";
    char buf1[10] = {0};
    char buf2[10] = {0};

    if(getStr1Str2(str, buf1, buf2)){
        printf("buf1 = %s, buf2 = %s\n", buf1, buf2);
    }else{
        printf("Error!");
    }
}

 

3、鍵值對(”key = valude”)字串,在開發中經常使用;

要求1:請自己定義一個介面,實現根據key獲取valude;40分

要求2:編寫測試用例。30分

要求3:鍵值對中間可能有n多空格,請去除空格30分

注意:鍵值對字串格式可能如下:

“key1=valude1”

“key2 =     valude2     ”

“key3  = valude3”

“key4        = valude4”

“key5   =   “

“key6   =“

“key7   =   “

 

int getKeyByValude(char *keyvaluebuf,  char *keybuf,  char *valuebuf);

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

//先去掉前面的空格,再去掉後面的空格
int trimSpace(char *inbuf){
    char *p = inbuf;
    //檢視有多少個空格
    int len = 0;
    while(p[len] == ' '){
        len++;
    }

    //將字串進行前移
    int i = len;
    while(p[i]){
        p[i - len] = p[i];
        i++;
    }
    p[i - len] = '\0';

    //去掉後面的字串
    for(int j = 0; j < strlen(p); j++){
        if(p[j] == ' '){
            p[j] = '\0';
            return 1;
        }
    }

    return 0;
}

//輸入字串,輸出key和value
int getValueByKey(char *str, char *keybuf, char *valuebuf){
    char *p = str;
    char *tempKeyBuf = keybuf;
    char *tempValueBuf = valuebuf;

    if(p == NULL){
        return 0;
    }

    int pos = 0;
    //首先確認等號的位置,將字串拆分成兩個部分
    for(int i = 0; i < strlen(p); i++){
        if(p[i] == '='){
            pos = i;
            break;
        }
    }

    strncpy(tempKeyBuf, p, pos);
    strncpy(tempValueBuf, p + pos + 1, strlen(p) - pos);

    trimSpace(tempKeyBuf);
    trimSpace(tempValueBuf);

    return 1;
}

int main(){
    char str1[] = "key2=     valude2     ";
    char keybuf[100] = {0};
    char valuebuf[100] = {0};

    if(getValueByKey(str1, keybuf, valuebuf)){
        printf("key = %s, value = %s\n", keybuf, valuebuf);
    }else{
        printf("ERROR.\n");
    }

    return 0;
}