qq_42658820 2019-03-22 09:44 采纳率: 28.6%
浏览 279
已采纳

如何在路径中的每段自动加入“”符号

比方说c:\123\456变成c:\"123"\"456"
c:\\"123\"\\"456\" 我知道字符串这样写但怎么写一个函数放进去就自动添加“”符号给路径中的每一段

  • 写回答

2条回答 默认 最新

  • threenewbee 2019-03-22 10:00
    关注

    如果问题解决,请点我回答做上角的采纳

    图片说明

    #include "stdio.h"
    #include "string.h"
    
    void repstr(char * des, char * src, char find, char * repwith)
    {
        int len = strlen(src);
        int rlen = strlen(repwith);
        int pos = 0;
        for (int i = 0; i < len; i++)
        {
            if (src[i] == find)
            {
                for (int j = 0; j < rlen; j++)
                {
                    des[pos++] = repwith[j];
                }
            }
            else
            {
                des[pos++] = src[i];
            }
        }
        des[pos] = '\0';
    }
    
    int main()
    {
        char * str = "c:\\123\\456\\";
        char * repwith = "\\\"";
        char buf[100];
        repstr(buf, str, '\\', repwith);
        printf("%s\n", buf);
        return 0;
    }
    

    // Q753041.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include "stdio.h"
    #include "string.h"
    
    void repstr(char * des, char * src, char find, char * repwith, char * firstwith = NULL, char * lastwith = NULL)
    {
        char buf1[100];
        char buf2[100];
        if (firstwith == NULL)
        {
            strcpy(buf1, &repwith[1]);
            firstwith = buf1;
        }
        if (lastwith == NULL)
        {
            strcpy(buf2, repwith);
            buf2[strlen(repwith) - 2] = '\0';
            lastwith = buf2;
        }
        int len = strlen(src);
        int rlen = strlen(repwith);
        int pos = 0;
        int first = 1;
        for (int i = 0; i < len; i++)
        {
            if (src[i] == find)
            {
                if (first)
                {
                    first = 0;
                    for (int j = 0; j < strlen(firstwith); j++)
                    {
                        des[pos++] = firstwith[j];
                    }
                }
                else
                {
                    for (int j = 0; j < rlen; j++)
                    {
                        des[pos++] = repwith[j];
                    }
                }
            }
            else
            {
                des[pos++] = src[i];
            }
        }
        for (int j = 0; j < strlen(lastwith); j++)
        {
            des[pos++] = lastwith[j];
        }
        des[pos] = '\0';
    }
    
    int main()
    {
        char * str = "c:\\123\\456\\789";
        char * repwith = "\"\\\"";
        char buf[100];
        repstr(buf, str, '\\', repwith);
        printf("%s\n", buf);
        return 0;
    }
    
    c:\"123"\"456"\"789"
    Press any key to continue . . .
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能