/**
* 在字符串中查找 匹配 sub字符串
*
* @param string 原有字符串
* @param subStr 需要匹配的字符串
* @param count 匹配到的字?jǐn)?shù)
*
* @return 結(jié)果碼
*/
int getCount(char* string,char *subStr, int* count){
int tempCount = 0;
int ret = 0;//返回的結(jié)果碼
if (string ==NULL||subStr==NULL||count==NULL) {
ret = -1;
printf("%d string ==NULL|| subStr==NULL||count==NULL",ret);
}
char *temStr = string;//盡量不要修改形參的值. 這里使用輔助指針
do {
temStr = strstr(temStr, subStr);//返回搜索到字符串的位置的 指針地址
if (temStr!= NULL) {
tempCount++;
temStr = temStr+strlen(subStr);
}else{
return 0;
}
} while (*temStr!='\0');
*count = tempCount;//使用指針作為函數(shù)的參數(shù),是C語言的精華所在.
return ret;
};
/**
* 拷貝數(shù)組字符串
*
* @param from 源數(shù)組
* @param to 目的數(shù)組
* @param num 從原數(shù)組中拷貝的個(gè)數(shù)
*/
void copy_str3(char* from,char* to,size_t num){
if (from == NULL|| to==NULL||num == 0) {
printf("from == NULL|| to==NULL");
return;
}
for(int i = 0;i<strlen(from);i++){
if (i<num) {
to[i] = from[i];
}else{
to[i] = '\0';
}
}
}
源數(shù)組和目的數(shù)組是一個(gè),這里會(huì)拷貝失敗,不同的源數(shù)組的存放區(qū),報(bào)的錯(cuò)誤也不一樣.這里自定義了一個(gè)拷貝數(shù)組字符串的方法copy_str3
int trimSpace2(char *inbuf){
int ret = 0;
// for
if (inbuf==NULL) {
return -1;
}
size_t i = 0,j = 0;
j = strlen(inbuf)-1;//去掉'\0' 字符
char* p = inbuf;
while (isspace(p[i])&&p[i]!='\0') { //這里不用指針 是因?yàn)樾枰@得i的值.
i++;
}
while (isspace(p[j])&&p[j]!= '\0') {
j--;
}
size_t count = j-i+1;
printf("dest-->%s\n",inbuf);
printf("source--->%s\n", inbuf+i);
copy_str3(inbuf+i, inbuf,count);
return ret;
};
int trimSpace(char *inbuf,char* outbuf){
int ret = 0;
// for
if (inbuf==NULL||outbuf==NULL) {
return -1;
}
size_t i = 0,j = 0;
j = strlen(inbuf)-1;//去掉'\0' 字符
char* p = inbuf;
while (isspace(p[i])&&p[i]!='\0') { //這里不用指針 是因?yàn)樾枰@得i的值.
i++;
}
while (isspace(p[j])&&p[j]!= '\0') {
j--;
}
size_t count = j-i+1;
strncpy(outbuf, inbuf+i, count); //從i 指針位置開始,拷貝count個(gè)字節(jié).
return ret;
};
int main(int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
//初始化
char *p = "abca;jf;adbacnabc aljafabc";
int count = 0;
do {
p = strstr(p, "abc");
if (p!= NULL) {
count++;
p = p+strlen("abc");
}
} while (*p!='\0');
printf("count--->%d\n",count);
////////////查重
char *test = "abca;jf;adbacnabc aljafabc";
int num = searchStr(test,"abc"); //如果這里還用p指針的話,會(huì)報(bào)錯(cuò). p指針已經(jīng)不再是字符串首地址
printf("num--->%d\n",num);
int num2 = 0;
////調(diào)用查重接口
int ret = getCount(test, "abc",&num2);
if (ret == 0) {
printf("num2--->%d\n",num2);
}
////去首尾空格 使用兩個(gè)字符串
char *trimStr = " dafadafadf ";
char buff[64]; //分配內(nèi)存,在堆上
trimSpace(trimStr,buff);
printf("trimStr -->%s\n",buff);
//
char mytrim[1024] = " afdfaddf ";//這種寫法變量分配在臨時(shí)存儲(chǔ)區(qū).在堆上
trimSpace2(mytrim);
printf("trimStr -->%s\n",mytrim);
char name[]={"Chinanet"},dest[20]={"ettgsgfgsffgsfgsdfg"};
strncpy(dest,dest+9,9); //
printf("%s\n",dest);
return 0;
}
要注意的:
char name[]={"Chinanet"},
dest[20]={"ettgsgfgsffgsfgsdfg"};
strncpy(dest,dest+9,9); //
printf("%s\n",dest);
字符串拷貝,
源數(shù)組,和目的數(shù)組 是同一個(gè)數(shù)組 使用strncpy 有時(shí)會(huì)拷貝失敗. 建議使用兩個(gè)數(shù)組
源數(shù)組的分配空間是在全局靜態(tài)區(qū),還是在堆區(qū).
char mytrim[1024] = " afdfaddf ";//這種寫法變量分配在臨時(shí)存儲(chǔ)區(qū).在堆上 可以修改內(nèi)存中的內(nèi)容
char *p = "abca;jf;adbacnabc aljafabc"; //在全局靜態(tài)區(qū) 不能重新寫入