C語(yǔ)言字符串刪除指定的元素 c語(yǔ)言程序中,從字符串s中刪除指定的字符c?
c語(yǔ)言程序中,從字符串s中刪除指定的字符c?沒(méi)有函數(shù),#include<stdio。H>void main(){TCHAR s[80],CH/tint I,J/tprintf(“input s
c語(yǔ)言程序中,從字符串s中刪除指定的字符c?
沒(méi)有函數(shù),#include<stdio。H>void main(){TCHAR s[80],CH/tint I,J/tprintf(“input string:”)-tgets(s)/tprintf(“input specified character to delete CH:”)-TCH=getchar()-tfor(I=J=0s[I]!=“0”I)/T/TIF(s[I]!=CH)s[J]=s[i]TS[J]=“0”-tprintf(”刪除字符%C后),字符串是:%sn”,CH,s)}
怎么用C語(yǔ)言刪除字符串中的指定字符?
用指針重新組織字符串?dāng)?shù)據(jù),刪除指針字符很方便。參考代碼如下:;#include<stdio。H>void del_Chr(char*s,char CH){char*t=s//目標(biāo)指針首先指向原始字符串頭,而(*s!=“)//遍歷字符串s{if(*s!=CH)//如果不刪除當(dāng)前字符,則將其保存到目標(biāo)字符串*t=*s//檢查下一個(gè)字符}*t=“//設(shè)置目標(biāo)字符串的結(jié)尾。}void main(){char str[]=“***abcde***fghi***”delchr(str,“*”)printf(“str=[%s]”,str)}
#include<stdio.h>
int delchar(char*p,char c)
{
char q=p
for(*p!=“0”p)
如果(*p!=c)*q=*p
*q=*p
}
int main()
{
char s[100],c
printf(“Input a string:n”)
獲取(s)
printf(“Delete char:”)
scanf(“%c”,&c)
delchar(s,c)
puts(s)
}