C字符串中删除指定字符几种算法
题如下图所示
<center></center>
题目意思很明显了,我们的思路其实也挺简单的,换句话说,删掉一个然后重构数组,补上那个空,一个个字符推进一格就行了嘛,不用想得太复杂(简单的来说就是偷懒)。
#include<stdio.h>
#include<string.h>
void delchar(char s[], char c);
int main(void)
{
char c;
char s[80];
printf("Input a string: ");
gets(s);
printf("Input a char: ");
scanf("%c", &c);
delchar(s, c);
printf("After deleted,the string is:%s", s);
return 0;
}
void delchar(char s[], char c)
{
int a, b, e;
e = strlen(s);
for(a=0; a < e; a ) {
if(s[a] == c) {
for(b = a; b < e; b )
s[b] = s[b 1];
a = a - 1;
}
}
}
算法二
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4326')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4326>#include <stdio.h>
char fun(char str[20],char ch)
{ int i,j;
for(i=0;str[i]!='\0';i )
if(str[i]==ch) {for(j=i;str[j]!='\0';j ) str[j]=str[j 1]; }
}
void main()
{ char str[20],ch;
printf("enter a string :");
gets(str);
printf("enter you want delete letter : ");
ch=getchar();
fun(str,ch);
printf("%s",str);
}
算法三
比如删除指定的字符在字符串中第一个出现的位置
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1555')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1555>void strdel( char* str, char ch ){
char *p = str;
while( *p )
{
if( *p==ch )
break;
}
if( *p )
{
while( *p )
{
*p==*(p 1);
p ;
}
}
}
程序是同学问我了之后我改的,所以不必太在意和我的风格不符=。=
您可能感兴趣的文章:
php字符串函数有哪些
python怎么删除字符串中的指定字符
php使用字符串截取函数从结尾删除字符串
php5 字符串处理函数汇总
php 字符串函数教程与实例代码
C#基类应用--字符串处理类
C字符串中删除指定字符几种算法
mysql 某个字段替换某个名词
PHP 5 String 函数
php整理字符串方法