比方说c:\123\456变成c:\"123"\"456"
c:\\"123\"\\"456\" 我知道字符串这样写但怎么写一个函数放进去就自动添加“”符号给路径中的每一段
#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 . . .
如果都是【c:\123\456】这种规律 笨办法函数里面文本替换:\ 替换成 \" 然后返回