美文网首页
字符串处理模型

字符串处理模型

作者: 带着白卡去旅行 | 来源:发表于2018-09-06 09:44 被阅读4次

两头堵模型
函数:
strstr
strlen(获取字符串长度)
strncpy(不替换型叠加);
strcpy (替换型叠加);

source:输入的字符串 newsource:输出的字符串

int  trimSpace(char *source,char*newsource)
{
    //两头堵模型
    int ncount = 0;
    char*p = source;
    int i = 0; int j = 0;
    if (source==NULL||newsource==NULL)
    {
        printf("func trimSpac() err:\n");
        return -1;

    }

    j = strlen(source) - 1;

    while ((isspace(p[i]))&&p[i]!='\0')
    {
        i++;
    }
    while ((isspace(p[j]))&&p[j]!='\0')
    {
        j--;
    }
    ncount = j - i + 1;
    strncpy(newsource, source + i, ncount);

    newsource[ncount] = '\0';//重要,最后的newsource补'\0'结尾,保证输出的正确性。
    return 0;
}

案例:
寻找母串中的字串
去除字符串前后的空格键,并输出字符串

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

//key="valude"

int  getKeyByVlue(char*keyvaluebuf, char*keybuf,char*valudebuf)
{
   char *p = NULL;
   int ret = 0;
   if (keyvaluebuf==NULL|| keybuf==NULL|| valudebuf==NULL)
   {
       return -1;
   }
   //查找key在不在母串中
   p = keyvaluebuf;
   p = strstr(p, keybuf);
   if (p==NULL)
   {
       return -1;
   }
   //让辅助指针变量 达到下一次检索的条件
   p = p + strlen(keybuf);
   //有无=号
   p = strstr(p, "=");
   if (p == NULL)
   {
       return -1;
   }
   p = p + strlen("=");
   //去除 等号后的空格
   ret = trimSpace(p, valudebuf);
   if (ret!=0)
   {
       printf("func trimSpace() err:%d ", ret);
       return ret;
   }
   return 0;
}

int  trimSpace(char *source,char*newsource)
{
   //两头堵模型
   int ncount = 0;
   char*p = source;
   int i = 0; int j = 0;
   if (source==NULL||newsource==NULL)
   {
       printf("func trimSpac()\n");
       return -1;
       

   }

   j = strlen(source) - 1;

   while ((isspace(p[i]))&&p[i]!='\0')
   {
       i++;
   }
   while ((isspace(p[j]))&&p[j]!='\0')
   {
       j--;
   }
   ncount = j - i + 1;
   strncpy(newsource, source + i, ncount);

   newsource[ncount] = '\0';
   return 0;
}


int main()
{
   int ret = 0;
   char buf[1024] = {0};
   int buflen = 0;

   char *keyandvalue = "key2=     valude2      ";
   char *key = "key2";

   ret =getKeyByVlue(keyandvalue, key,buf,&buflen);
   if (ret!=0)
   {
       printf("func getKeyByVlue() err:%d\n",ret);
       return ret;
   }

printf("buf %s\n",buf);
system("pause");
return ret;
}

案例2
巧妙的字符串提取
a1b2c3d4

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


void getstr(char*source, char*str_old,char* str_new)
{
    char*p = source;
    if (source==NULL||str_old==NULL||str_new==NULL)
    {
        int num =-1;
        printf("func getstr() check (source==NULL||str_old==NULL||str_new==NULL)..erro:%d", num);
        return num;

    }

    while (*p)
    {
        
        *str_old=*p;
        p++;
        str_old++;
        *str_new=*p;
        p++;
        str_new++;
    }
    return 0;
}




int main4()
{

    char source[] = "1a2b3c4d";
    char str_single[100] = " ";
    char  str_double[100] = " ";
    getstr(source, str_single, str_double);
    printf("source %s\n",source);
    printf("str_single %s\n", str_single);
    printf("str_double %s\n",str_double);

    printf("hello...\n");

    system("pause");
}

相关文章

网友评论

      本文标题:字符串处理模型

      本文链接:https://www.haomeiwen.com/subject/ggutgftx.html