#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// 替換字符串
void replace_str(char* text, char sp_ch, char re_ch);
int main() {
char input[11] = "2020-10-10";
replace_str(input, '-', '/');
printf("%s \n", input);
return 0;
}
// 替換字符串(sp_ch 替換之前的字符, re_ch 即將替換的字符)
void replace_str(char* text, char sp_ch, char re_ch) {
int len = strlen(text);
// 動態創建copy之後的字符創
char* copy = (char*)malloc(len + 1);
for (int i = 0; i < len; i++)
{
// 獲取當前的char
char ch = text[i];
if (ch == sp_ch)
copy[i] = re_ch;
else
copy[i] = ch;
}
// 結束
copy[len] = 0;
// 賦值給傳進來的字符串
strcpy(text, copy);
// 釋放動態創建的內存
free(copy);
return text;
}
C/C++ 替換字符串(指定char)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.