C語言字元串例題
第一題:函數實作字元串兩端去除空格字元
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
/**
*有一個字元串開頭或結尾含有n個空格 (” abcdefgdddd ”),
*欲去掉前後空格,傳回一個新字元串。
*要求1:請自己定義一個接口(函數),并實作功能;
*要求2:編寫測試用例。
*int trimSpace(char *inbuf, char *outbuf);
*/
/**
* func:實作字元串的前後空格去除
* param:
* inbuf:輸入字元串
* outbuf:輸出字元串
* return:
* 錯誤傳回1.正常執行傳回0
*/
int trimSpace(char *inbuf, char *outbuf)
{
if (inbuf == NULL || outbuf == NULL)
{
return -1;
}
char *in = inbuf;
char *out = outbuf;
int length = strlen(in) - 1;
char *end = in + length;
while (isspace(*in))
{
in++;
}
while (isspace(*end))
{
end--;
}
length = end - in + 1;
strncpy(out, in, length);
return 0;
}
int main()
{
char buf[] = " abcdefgdddd ";
char bufOut[100] = { 0 };
trimSpace(buf, bufOut);
printf("bufOut = %s\n", bufOut);
printf("\n");
system("pause");
return 0;
}
第二題:程式設計實作每隔一位取字元子串
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/**
* 有一個字元串“1a2b3d4z”;
* 要求寫一個函數實作如下功能:
* 功能1:把偶數位字元挑選出來,組成一個字元串1。
* 功能2:把奇數位字元挑選出來,組成一個字元串2。
* 功能3:把字元串1和字元串2,通過函數參數,傳送給main,并列印。
* 功能4:主函數能測試通過。
* int getStr1Str2(char *source, char *buf1, char *buf2);
*/
int getStr1Str2(char *source, char *buf1, char *buf2)
{
if (source == NULL || buf1 == NULL || buf2 == NULL)
{
return - 1;//代表異常
}
char *src = source;
char *out1 = buf1;
char *out2 = buf2;
while (*src)
{
*out1++ = *src++;
*out2++ = *src++;
}
//最後給字元串末尾添加0
*out1 = 0;
*out2 = 0;
return 0;
}
int main()
{
char buf[] = "1a2b3c4d";
char buf1[20];
char buf2[20];
getStr1Str2(buf, buf1, buf2);
printf("buf1 = %s\n", buf1);
printf("buf2 = %s\n", buf2);
printf("\n");
system("pause");
return 0;
}
第三題:鍵值對問題
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
//注: char* strstr(const char*src,const char* subsrc);傳回字元串第一次出現的位置
/**
* 鍵值對(“key = value”)字元串,在開發中經常使用
* 要求1:請自己定義一個接口,實作根據key擷取.
* 要求2:編寫測試用例。
* 要求3:鍵值對中間可能有n多空格,請去除空格
* 注意:鍵值對字元串格式可能如下:
* "key1 = value1"
* "key2 = value2"
* "key3 = value3"
* "key4 = value4"
* "key5 = "
* "key6 ="
* int getKeyByValue(char *keyvaluebuf, char *keybuf, char *valuebuf, int * valuebuflen);
*/
//如果函數使用在其定義之前,需要在使用前對函數進行聲明
int trimSpace(char *inbuf, char *outbuf);
int getKeyByValue(char *keyvaluebuf, char *keybuf, char *valuebuf, int * valuebuflen)
{
if (keyvaluebuf == NULL || keybuf == NULL || valuebuf == NULL || valuebuflen == NULL)
{
return -1;
}
//查找pos第一次出現的位置
char* pos = strstr(keyvaluebuf, keybuf);
if (pos == 0)
{
return -2;//此時代表目前無此鍵
}
char* valuebufTmp = strstr(keyvaluebuf, "=")+1;
trimSpace(valuebufTmp, valuebuf);
*valuebuflen = strlen(valuebuf);
}
int trimSpace(char *inbuf, char *outbuf)
{
if (inbuf == NULL || outbuf == NULL)
{
return -1;
}
char *in = inbuf;
char *out = outbuf;
int length = strlen(in) - 1;
char *end = in + length;
while (isspace(*in))
{
in++;
}
while (isspace(*end))
{
end--;
}
length = end - in + 1;
strncpy(out, in, length);
*(out + length) = 0;
return 0;
}
int main()
{
char valuebuf[20];
int valuebuflen = 0;
getKeyByValue("key1 = valude1 ", "key1", valuebuf, &valuebuflen);
printf("value = %s\n", valuebuf);
printf("valuebuflen = %d\n", valuebuflen);
printf("\n");
system("pause");
return 0;
}