c语言: 在字符串某位置插入字符串

在字符串某位置插入字符串

 

#if 1

#include
/*      */
void insert(char *s,char *q,int n)  //insert a string(q) to a  string.(s) n is the position will to insert.  
{
int i=0;
char *str,strcp[60];
str=strcp;
    for (i=0;*s!='\0';i++)  //没到字符串结尾
    {
       if (i==n-1)    //第n-1个值
       {
            while(*q!='\0')  //q的值不是字符串结尾
            {
             str[i]=*q;
             q++;
             i++;
            }
        }
str[i]=*s;
s++;
    }
str[i]='\0';
printf("%s",str);
}
int  main()
{
char *strin,*str ;
int i;
str ="how are you ";
strin="yes,i am fine.";
printf("the origin string is: %s \n",str);
printf("please input a position \n");
scanf ("%d",&i);
insert(str,strin,i);
}

 

#endif

你可能感兴趣的:(C语言)