1、有一個字符串開頭或結尾含有n個空格(” abcd1234 ”),欲去掉前後空格,返回一個新字符串。
要求1:請自己定義一個接口(函數),並實現功能;70分
要求2:編寫測試用例。30分
int trimSpace(char *inbuf, char *outbuf);
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getnewbuf(char *inbuf, char* outbuf, int *outlen)
{
char *p = inbuf;
int i = 0;
int j = 0;
int tmpcount = 0;
j = strlen(p) - 1; //因爲數組從0開始,-1,纔是指向於結尾最後一個字符的指針
printf("j: %d\n", j);
while (isspace(p[i]) && p[i] != '\0') {
i++;
}
printf("i: %d\n", i);
while (isspace(p[j]) ) {
j--;
}
printf("j: %d\n", j);
tmpcount = j - i + 1; //j - i + 1; ==》 17-10 = 7 ==》 7+1 = 8, (數組開頭自身1也要加上)
printf("tmpcount: %d\n", tmpcount); //
strncpy(outbuf, p+i, tmpcount);
*outlen = tmpcount;
return 0;
}
int main01(int argc, const char * argv[])
{
char *inbuf = " abcd1234 ";
printf("inbuf:%s\n", inbuf);
char outbuf[] = {0};
int outlen;
getnewbuf(inbuf, outbuf, &outlen);
printf("outbuf: %s, out buf lenth: %d\n", outbuf, outlen);
printf("Hello, World!\n");
return 0;
}