字符串替换

题目:

写一个函数char* replaceF(char *str)将一个字符串中的F替换为fly并返回新的字符串。

比如:传入字符串“123FabcF456”,并返回字符串“123flyabcfly456”(不能使用c库字符串处理函数)。

#include 
#include
#include 

//核心函数
char* replaceF(char *str){
	char *str1=(char *)malloc(20);//初始化	
	char *str3="F";
	char *temp = str1; //把首地址存下来
	while(*str != '\0'){	
		if(*str == *str3){	
			char *str2="fly"; //替换变量 
			while(*str2 != '\0'){				
				*str1++ = *str2++;
			}					
			*str++;
		}
		*str1++ = *str++;
	}
	return temp;	
}

//控制台
int main(){
	char str[12]= "123FabcF456";	
	char *lkm=replaceF(str);
	printf("%s",lkm);
	return 0;
}

你可能感兴趣的:(杂记,c语言)