當前位置:首頁 » 編程語言 » 串的插入c語言

串的插入c語言

發布時間: 2023-09-10 19:50:23

c語言在指定位置插入字元串

如下


代碼
#include
int main (void)
{
char s1[80],s2[80],k,*p1,*p2,*pnew,*s3;
int n1,n2;n1=n2=0;
gets(s1);gets(s2);scanf("%c",&k);
p1=s1;p2=s2;
while(*p1)
{ n1++;p1++; }
while(*p2)
{ n2++;p2++;}
pnew=(char *)malloc(sizeof(char)*(n1+n2+1));
if(pnew==NULL)
{printf("分配內存失敗! ");exit(0);}
p1=s1;p2=s2;s3=pnew;
while(*p1)
{
if(*p1!=k)
{*pnew=*p1;p1++; pnew++;}
else if(*p2)
{*pnew=*p2;p2++;pnew++;}
else
{*pnew=*p1;p1++;pnew++;}
}
*pnew=''

puts(s3);
free(s3);
return 0;
}

⑵ 用C語言編寫一個在字元串中插入一個字元的程序

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#defineN100

voidInsert(char*s);

intmain()

{

charstr[N];

printf("Inputastring:");

gets(str);

Insert(str);

printf("Insertresults:%s ",str);

return0;

}

voidInsert(char*s)

{

charstr[N];

char*t=str;

strcpy(t,s);

for(;*t!='';s++,t++)

{

*s=*t;

s++;

*s='';

}

*s='';/*在字元串s的末尾添加字元串結束標志*/

}

(2)串的插入c語言擴展閱讀

C語言通過定義一個char類型的二維數組實現,通過二維數組的行索引可得到數組中的每個字元串,列的大小限定了每個字元串所能包含的最大字元個數,所以採用這種定義方式時,列的大小必須不能小於數組所有字元串的最大長度。

C語言編程定義一個字元串的數組:

str={

「IloveC.」,

「IloveC++.」,

「IloveJAVA.」,

「IlovePython.」,

「IloveLabVIEW.」

}

⑶ 將一個字元串插入到另一個字元串的指定位置的題目(用C語言解決)

1、參數2去掉與參數1相同的字元串。

⑷ 用C語言怎麼寫個 字元串插入函數

程序的大體思路可以是這樣:
str1是原字元串,str2是待插入的字元串,position是待插入的位置,我們可以這樣,用一個指針p_cur指向字元串1 str1中的待插入位置position,另一個指針p_end指向字元串1 str1的尾部,每次插入字元前,把str1中從當前位置開始一直到結束字元全部後移一個位置,空出當前位置,然後把要插入的字元放進這個位置,這樣就完成了一個字元的插入,重復這個步驟,直到str2被完全插入。
代碼如下:
#include <stdio.h>
#include <string.h>
void insert_str(char str1[],char str2[],int position)
{
/*
insert_str()函數
功能:將字元串str2插入到str1的position位置處
參數:char str1,char str2 ,int position
返回值:無
*/
int i;
char *p_end,*p_cur,*p;/*p_end指向第一個字元串的尾部,p_cur指向被插入的位置*/
p_end=str1+strlen(str1)-1;
p_cur=str1+position-1;
for(i=0;str2[i]!='\0';i++)
{
for(p=p_end;p>=p_cur;p--)
{
*(p+1)=*p;/*從p_cur到p_end的全部元素後移一個位置,此時p_cur指向的位置就空出來了*/
}
*p_cur=str2[i];/*把字元串2中的字元插入空出來的位置*/
p_cur++;/*p_cur下移一個位置*/
p_end++;/*多了一個字元,因此p_end也下移一個位置*/
}
}

void main()
{
char s1[100],s2[20];
int position;
printf("輸入字元串1:\n");
gets(s1);
printf("輸入插入位置:");
do
{
scanf("%d",&position);
while(getchar()!='\n');/*這一句可以把輸入position的時候輸入的回車去掉*/
}while(position<0||position>strlen(s1));
printf("輸入字元串2:\n");
gets(s2);
insert_str(s1,s2,position);
printf("字元串被插入後變成:\n");
puts(s1);
}

⑸ c語言程序編制一個將一個字元串插入到另一個字元串的指定位置的函數。

intmain()
{
chara[100],b[100],*x,*y;
inti;
printf("輸入字元串1:");
scanf("%s",a);
printf("輸入字元串2:");
scanf("%s",b);
printf("將字元串1插入到字元串2的第幾個字元後:");
scanf("%d",&i);
x=a;
y=b+i;
while((*y++=*x++)!='');
printf("%s",b);
return0;
}

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:432
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:743
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:537
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:146
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:235
java駝峰 發布:2025-02-02 09:13:26 瀏覽:651
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:538
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726