
void fun (char s1[],char s2[]){int i,j;for (i=0;s1[i] !=’\0’; i++); /求出的i为pA
字符
的总长度,包括结束标记位/for (j=0;s2[j] !=’\0’; j++)s1[i++]=s2[j]; /将pB
字符串
连在pA字符串的后面/
s1[i]='\0’; /在字符串最后加上结束标记符/ }
函数
:
字符串输出函数puts格式:puts (字符数组名) 功能:把字符数组中的字符串输出到显示器。
2字符串输入函数gets格式:gets (字符数组名) 功能:从标准输入设备键盘上输入一个字符串。本函数得到一个函数值,即为该字符数组的首地址。
3字符串连接函数strcat格式:strcat (字符数组名1,字符数组名2) 功能:把字符数组2中的字符串连接到字符数组1 中字符串的后面,并删去字符串1后的串标志“”。
4字符串拷贝函数strcpy格式:strcpy (字符数组名1,字符数组名2) 功能:把字符数组2中的字符串拷贝到字符数组1中。串结束标志“”也一同拷贝。
5字符串比较函数strcmp格式:strcmp(字符数组名1,字符数组名2) 功能:按照ASCII码顺序比较两个数组中的字符串,并由函数返回值返回比较结果。 6测字符串长度函数strlen格式:strlen(字符数组名) 功能:测字符串的实际长度(不含字符串结束标志‘’) 并作为函数返回值。
-数组
因为数组元素是从str1[0]开始的,china五个字符正好安排到str[4];
你把k=i,改成k=i+1后,就是从str1字符串的\o后加代码,肯定输出时就输出到\o,所以,就是china了;1、首先输入代码:
#include <stringh>
#include <stdioh>
/
参数:
originalString[] :原始字符串
key[] : 待替换的字符串
swap[] : 新字符串
/
void replace(char originalString[], char key[], char swap[]){
int lengthOfOriginalString, lengthOfKey, lengthOfSwap, i, j , flag;
char tmp[1000];
2、然后输入:
//获取各个字符串的长度
lengthOfOriginalString = strlen(originalString);
lengthOfKey = strlen(key);
lengthOfSwap = strlen(swap);
for( i = 0; i <= lengthOfOriginalString - lengthOfKey; i++){
flag = 1;
//搜索key
for(j = 0; j < lengthOfKey; j ++){
if(originalString[i + j] != key[j]){
flag = 0;
break;
}
}
3、然后输入:
//如果搜索成功,则进行替换
if(flag){
strcpy(tmp, originalString);
strcpy(&tmp[i], swap);
strcpy(&tmp[i + lengthOfSwap], &originalString[i + lengthOfKey]);
strcpy(originalString, tmp);
i += lengthOfSwap - 1;
lengthOfOriginalString = strlen(originalString);
}
}
}
4、然后输入:
/
main function
/
int main(){
char originalString[1000] = {"abcfffffabcfffffabcfffff"};
char key[] = {"abc"};
char swap[] = {"aabbcc"};
replace(originalString, key, swap);
printf("%s\n", originalString);
return 0;
}欢迎分享,转载请注明来源:内存溢出

