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

c語言插入字元串

發布時間: 2023-01-03 18:51:55

『壹』 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語言怎麼寫個 字元串插入函數

程序的大體思路可以是這樣:
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語言編寫一個在字元串中插入一個字元的程序

#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的末尾添加字元串結束標志*/

}

(3)c語言插入字元串擴展閱讀

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

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

str={

「IloveC.」,

「IloveC++.」,

「IloveJAVA.」,

「IlovePython.」,

「IloveLabVIEW.」

}

『肆』 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;
}

『伍』 在C語言中怎麼輸入一個字元串

在c語言中存儲一個字元串,一般有兩種方法,一種是字元指針,一種是使用字元數組。比如:
const char *str = "hello"; //使用字元串指針
const char str[] = "hello"; //使用字元數組
如果保存的字元串,需要修改。一般使用字元數組。比如:
char path[256] = "c:\\windows\\";
strcat(path, "system32");

『陸』 c語言輸入一個字元串,如何在指定位置插入一個字元

也許用鏈表比較好實現,下面是用數組實現的程序:


#include<stdio.h>


#include<string.h>


main()


{ int i,j,k,m,n;


char a[100],b[100],c;


gets(a);


gets(b);


c=getchar();


m=strlen(a);


for(i=0;i<m;i++)


{if(a[i]==c)<br/> break;<br/> }


if(i>=m)


{printf("出錯! ");<br/> return;<br/> }


n=strlen(b);


k=m;


for(j=m+n;j>i;j--,k--)


a[j]=a[k];


for(j=i,k=0;j<n+i;j++,k++)


a[j]=b[k];


printf("%s ",a);


}

『柒』 C語言編程:將一個字元插入到字元串中的指定位置

Description:把字元串截取成2段,將指定字元插入,讓你將它們再連接。
#include
<stdio.h>
#include
<string.h>
int
main(void)
{
char
str[256],s1[256],s2[256];
char
ch;
int
i,j=0,k=0;
printf("輸入字元串:\n");
gets(str);
printf("輸入要插入的字元和位置:\n");
scanf("%c%d",ch,n);
for(i=0;i<n-1;i++)
{
s1[j++]=str[i];
}
s1[j]=ch;//插入指定字元
for(j=i;j<strlen(str);j++)
{
s2[k++]=str[j];
}
strcat(s1,s2);
puts(s2);
return
0;
}

『捌』 c語言中如何將一字元串插入另一個字元串中

#include<stdio.h>
void main()
{
void insert(char a[],char b[],int t);
char a[100],b[100];
int t,i,j;
printf("input a string:\n");
gets(a);
printf("inut b string:\n");
gets(b);
printf("input the position to insert:\n");
scanf("%d",&t);
if(t<=0 || t>strlen(a)+1)
{
printf("wrong!.input again");
scanf("%d",&t);
}
insert(a,b,t);
}

void insert(char a[],char b[],int t)
{
int len1,len2;
int i,j,k,l;
char c[100];
len1=strlen(a);
len2=strlen(b);
for(i=0;i<t-1;i++)
c[i]=a[i];
for(j=0;j<len2;j++)
c[i+j]=b[j];
l=0;
for(k=t-1;k<len1;k++)
{
c[i+j+l]=a[k];
l++;
}
c[i+j+l]='\0';
puts(c);
}
另外,站長團上有產品團購,便宜有保證

『玖』 C語言從字元串的指定位置中插入指定字元,要簡單易懂


#include<stdio.h>
#include<string.h>
intmain(void)
{
chara[30];
charb,c;
char*pt;
char*i;
printf("請輸入基本字元串:");
scanf("%s",&a);
getchar();
while(1)
{
pt=NULL;
while(pt==NULL)
{
printf("請輸入插入位置左側字元:");
scanf("%c",&b);
getchar();
pt=strchr(a,b);
}
printf("請輸入將插入字元:");
scanf("%c",&c);
getchar();
for(i=a+strlen(a)+1;i>pt;i--)
{
if(i==pt+1)
{
a[i-a]=c;
break;
}
a[i-a]=a[i-a-1];
}
printf("結果:%s ",a);
}
while(1);
}

『拾』 C語言字元插入

#include<stdio.h>
#include<string.h>
#defineN100
voidinsertChar(chars[],charc)
{
intn,i,j;
for(i=0;s[i]!='';i++)
{
if(s[i]>='0'&&s[i]<='9')
{
n=strlen(s);
s[n+1]='';
for(j=n;j>i+1;j--)
{
s[j]=s[j-1];
}
s[i+1]=c;
}
}
}

intmain(intargc,char*argv[])
{
charstr[N];
gets(str);
insertChar(str,getchar());
puts(str);
return0;
}

#include<stdio.h>
#include<string.h>
#defineN100
voidinsertChar(chars[],charc)
{
intn,i,j;
for(i=0;s[i]!='';i++)
{
if(s[i]>='0'&&s[i]<='9')
{
n=strlen(s);//計算字元串長度,不包括''
for(j=n;j>i;j--)
{
s[j+1]=s[j];//以s[i]為界限,將s[i+1]至結尾後移
}
s[i+1]=c;
}
}
}

intmain(intargc,char*argv[])
{
charstr[N];
gets(str);//非常危險的函數,從鍵盤輸入一字元串,以回車結束,並將回車替換為''
insertChar(str,getchar());
puts(str);
return0;
}

熱點內容
ntp伺服器怎麼搭建 發布:2025-07-05 02:51:53 瀏覽:768
譚浩強c語言基礎 發布:2025-07-05 02:51:45 瀏覽:775
外地卡密碼忘了怎麼辦 發布:2025-07-05 02:50:10 瀏覽:185
電腦配置點評怎麼選 發布:2025-07-05 02:39:23 瀏覽:1002
如何配置與鋅反應的稀硫酸 發布:2025-07-05 02:39:20 瀏覽:937
php分割文件 發布:2025-07-05 02:22:15 瀏覽:478
sql平均成績語句 發布:2025-07-05 02:11:41 瀏覽:277
java離線 發布:2025-07-05 02:11:35 瀏覽:66
php變數賦值給變數 發布:2025-07-05 02:10:56 瀏覽:558
javaequals方法 發布:2025-07-05 01:57:23 瀏覽:98