c语言-字符串操作

2 篇文章 0 订阅
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

/*
将to的路由器IP更换为from的SIP Server IP
*/
int main()
{
    char *from ="\"Extension 1010\" <sip:1010@192.168.1.235>";
    char *to = "<sip:1006@192.168.1.91:58926;transport=udp>";

    if(indexOf(to,";") > 0){
        int fromLen,fromStart,toStart,toEnd;
        char *fromIP;

        //1.取出from的IP
        printf("to has ; in the string\n");
        fromLen = strlen(from);
        if(indexOf(from,">") > 0){
            fromLen = fromLen-1;
        }
        fromStart = indexOf(from,"@");
        fromIP= (char *)malloc(fromLen-fromStart);
        substring(fromIP,from,fromStart,fromLen);
        printf("from ip is %s\n",fromIP);

        //2. 取出to的sip account
        toStart = indexOf(to,"sip:");
        toEnd = indexOf(to,"@");
        if(toEnd > toStart){
            char *toAccout;
            toAccout = (char *)malloc(toEnd-toStart);
            substring(toAccout,to,toStart,toEnd);
            printf("to account=%s start=%d,end=%d\n",toAccout,toStart,toEnd);

            to = (char *)malloc(strlen(fromIP) + strlen(toAccout) + 1);
            strcpy(to, toAccout);
            strcat(to, fromIP);
            printf("new to address is %s\n",to);
            free(toAccout);
        }

        free(fromIP);
    }

    getch();
    return 0;
}


void substring(char *dest,char *src,int start,int end)
{
    int i=start;
    if(start>strlen(src))return;
    if(end>strlen(src))
        end=strlen(src);
    while(i<end)
    {
        dest[i-start]=src[i];
        i++;
    }
    dest[i-start]='\0';
    return;
}

int indexOf(char *str1,char *str2)
{
    char *p=str1;
    int i=0;
    p=strstr(str1,str2);
    if(p==NULL)
        return -1;
    else{
        while(str1!=p)
        {
            str1++;
            i++;
        }
    }
    return i;
}
因为第一次使用C语言,所以将一些基本注意事项记录一下:
1.所有声明必须紧跟在 { 后面
2.使用malloc动态分析内存,使用free释放
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值